Skip to content

Bad types in defineField & Zod default values #5037

@biwchi

Description

@biwchi

This schema uses default values.

export const buttonSchema = object({
  id: number().default(uniqueID),
  text: string().nonempty().trim().default(''),
  background: string().nonempty().default('#000000'),
  color: string().nonempty().default('#ffffff'),
})

useForm({
  validationSchema: toTypedSchema(popupSchema.merge(object({
    buttons: buttonSchemaWithUrl.array().default([]),
  }))),
})

When I define the buttons field, I get these types.

const [buttons] = defineField('buttons', {})

// type 
const buttons: Ref<{
    url: string;
    id?: number | undefined;
    text?: string | undefined;
    background?: string | undefined;
    color?: string | undefined;
}[] | undefined, {
    url: string;
    id?: number | undefined;
    text?: string | undefined;
    background?: string | undefined;
    color?: string | undefined;
}[] | undefined>

But when i remove default values

const buttons: Ref<{
    id: number;
    text: string;
    background: string;
    color: string;
    url: string;
}[] | undefined, {
    id: number;
    text: string;
    background: string;
    color: string;
    url: string;
}[] | undefined>

Is this expected behavior? I want default values for my form and none-null types

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions