Skip to content

zenstack/zod generates incompatible types for Optional fields when comparing Types generated by zenstack/ormand zenstack/zod #2640

@iPommes

Description

@iPommes

Description and expected behavior
When I'm using the zenstack/zod generator to generate my return types (for orpc) and then comparing them to the Types generated by the $ModelResult optional fields have incompatibilites.

Screenshots
Example schema to reproduce:

type BasicFields {
    id        String   @id @default(uuid())

    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
    metaData  Json     @default("{}")
}

model Test with BasicFields {
    name String
    description String?
}

The types generated by zenstack/zod are:

type Test = {
    id: string;
    createdAt: Date;
    updatedAt: Date;
    metaData: any;
    name: string;
    description?: string | null | undefined; //This seems to be the problem
}

but when I'm passing "test" (imported directly form the model.ts which generates:

export type Test = $ModelResult<$Schema, "Test">;
type Test = {
    id: string;
    createdAt: Date;
    updatedAt: Date;
    metaData: JsonValue;
    name: string;
    description: string | null; //This seems to be the problem
}

the compiler complains about an incompatible type on all optional fields because null and undefined are incompatible.

Argument of type '{ id: string; createdAt: Date; updatedAt: Date; metaData: any; name: string; description?: string | null | undefined; }' is not assignable to parameter of type 'FlatModelResult<SchemaType, "Test", undefined, QueryOptions<SchemaType>>'.
  Types of property 'description' are incompatible.
    Type 'string | null | undefined' is not assignable to type 'string | null'.
      Type 'undefined' is not assignable to type 'string | null'.

This is a representation of code that I'm using:

const testSchema = zodFactory.makeModelSchema("Test")
testFunction({} as z.infer<typeof testSchema>) //Calling the function with the return type returned by my orpc contract

function testFunction(test: Test) {}

I have already tried to set the strictNullChecks: false but then suddenly everything else breaks and no type of $ModelResult gets generated.

Environment (please complete the following information):

  • ZenStack version: 3.6.4
  • Database type: PostgreSQL
  • Node.js/Bun version: Bun v1.3.13
  • Package manager: bun

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions