Skip to content

Type error on create when a relation with a default value is implicitly undefined #2567

@DomZem

Description

@DomZem

Description
I'm encountering a TypeScript type error when using the create method on a model that has a required relationship.

The issue occurs when I first provide the foreign key for the relation (e.g., authorId) and then use the spread operator (...) to pass in the remaining data for the creation. Even though the relation is satisfied by the foreign key, the type system flags an error because the spread object's type includes the optional relation field (e.g., author), which it believes is incompatible.

Reproduction
I have created a minimal repository to demonstrate the issue:
https://github.com/DomZem/zenstack-not-null

The relevant code is in the post.builder.ts file, specifically within the build method.
Example usage of that class is in seed.ts

Expected Behavior
The create method's types should correctly infer that the relation is satisfied when a foreign key is provided. It should not produce a type error when other optional data is spread, even if that data's type includes the optional relation field.

Environment

  • ZenStack version: 3.5.6
  • Database type: Postgresql
  • Node.js/Bun version: 20.19.6
  • Package manager: pnpm

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