Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty interfaces' extends statements are ignored #108

Closed
NexZhu opened this issue Jan 11, 2023 · 1 comment · Fixed by #109
Closed

Empty interfaces' extends statements are ignored #108

NexZhu opened this issue Jan 11, 2023 · 1 comment · Fixed by #109

Comments

@NexZhu
Copy link
Contributor

NexZhu commented Jan 11, 2023

Bug description

For example, in the following code Object is an empty interface extending BaseObject, which has an id field.

export interface BaseObject {
  "id": string;
}
export interface Object extends BaseObject {}

Running ts-to-zod fails with:

✖ Validating generated types
 ›   Error: 'objectSchema' is not compatible with 'Object':
 ›   Argument of type '{}' is not assignable to parameter of type 'Object'.
 ›     Property '"id"' is missing in type '{}' but required in type 'Object'.
error Command failed with exit code 2.

And running with --skipValidation generates:

// Generated by ts-to-zod
import { z } from "zod";

export const baseObjectSchema = z.object({
  id: z.string(),
});

export const objectSchema = z.object({});

Expected output:

export const objectSchema = baseObjectSchema;

The problematic code is in generateZodSchema.ts, schemaExtensionClauses is handled only when properties.length > 0:

image

Versions

  • Typescript: v4.8.4
@NexZhu
Copy link
Contributor Author

NexZhu commented Jan 11, 2023

With #109 applied, there are still some issues with validation.

Input:

export namespace std {
  export interface BaseObject {
    "id": string;
  }
  export interface $Object extends BaseObject {}
}

export interface Base extends std.$Object {
  "createdAt": Date;
}

The output is actually correct with --skipValidation:

// Generated by ts-to-zod
import { z } from 'zod'

export const stdBaseObjectSchema = z.object({
  id: z.string(),
})

export const stdObjectSchema = stdBaseObjectSchema.extend({})

export const baseSchema = stdObjectSchema.extend({
  createdAt: z.date(),
})

But ts-to-zod gave me a validation error:

✖ Validating generated types
 ›   Error: 'Base' is not compatible with 'baseSchema':
 ›   Argument of type 'Base' is not assignable to parameter of type '{ id: string; createdAt: Date; }'.
 ›     Property '"id"' is missing in type 'Base' but required in type '{ id: string; createdAt: Date; }'.

NexZhu added a commit to daotl/ts-to-zod that referenced this issue Jan 11, 2023
fabien0102 pushed a commit that referenced this issue Jan 30, 2023
* fix: empty interfaces' extends statements are ignored #108

* style: trim trailing whitespace

* test: add a test case for empty interfaces' extends statements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant