Skip to content

Type 'string | undefined' is not assignable to type 'string #1957

@khanakia

Description

@khanakia

Describe the bug

Ref ZOD: colinhacks/zod#4938 (comment)

The error occurs when used the .default.

I know i dont need to use the .default() but the thing is i am using the schema on the server side and used the same shared schema on the client side too to validate my form

"use client"

import { useForm } from "@tanstack/react-form"
import { z } from "zod/v4"

export const myschema = z.object({
  url: z
    .url("Please enter a valid URL")
    .default("https://countries.trevorblades.com/"),
})

export type TMySchema = z.infer<typeof myschema>

export function GraphQLExplorerForm() {
  const form = useForm({
    defaultValues: {
      url: "https://countries.trevorblades.com/",
    } as TMySchema,
    validators: {
      onChange: myschema,
    },
  })

  return null
}
Type 'ZodObject<{ url: ZodDefault<ZodURL>; }, $strip>' is not assignable to type 'FormValidateOrFn<{ url: string; }> | undefined'.
  Type 'ZodObject<{ url: ZodDefault<ZodURL>; }, $strip>' is not assignable to type 'StandardSchemaV1<{ url: string; }, unknown>'.
    The types of ''~standard'.types' are incompatible between these types.
      Type 'Types<{ url?: string | undefined; }, { url: string; }> | undefined' is not assignable to type 'StandardSchemaV1Types<{ url: string; }, unknown> | undefined'.
        Type 'Types<{ url?: string | undefined; }, { url: string; }>' is not assignable to type 'StandardSchemaV1Types<{ url: string; }, unknown>'.
          The types of 'input.url' are incompatible between these types.
            Type 'string | undefined' is not assignable to type 'string'.
              Type 'undefined' is not assignable to type 'string'.ts(2322)
Image

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-ihx6xoop?file=src%2Findex.tsx

Steps to reproduce

Use the code snippet

Expected behavior

It should pass

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

macOS 26, chrome 143.0.7499.147

TanStack Form adapter

react-form

TanStack Form version

1.27.6

TypeScript version

5.9.3

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions