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

validation fails with 3.7.1. when referencing imported type #207

Closed
schiller-manuel opened this issue Feb 19, 2024 · 6 comments
Closed

validation fails with 3.7.1. when referencing imported type #207

schiller-manuel opened this issue Feb 19, 2024 · 6 comments

Comments

@schiller-manuel
Copy link
Collaborator

schiller-manuel commented Feb 19, 2024

After #205 was merged, validation still fails in v3.7.1 for the following reproducer:

502b7c0

✖ Validating generated types
 ›   Error: 'barSchema' is not compatible with 'Bar':
 ›   Argument of type '{ [x: string]: any; hello?: { foo: string; } | undefined; desc?: any; }' is not assignable to parameter of type 'Bar'.
 ›     Types of property 'hello' are incompatible.
 ›       Type '{ foo: string; } | undefined' is not assignable to type 'Foo'.
 ›         Type 'undefined' is not assignable to type 'Foo'.

@tvillaren please have a look

tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Feb 19, 2024
@tvillaren
Copy link
Collaborator

Yes, it's the case that we're trying to had handling for (not the one originally mentioned in #203)

Just finished #204 to cover this case.

@schiller-manuel
Copy link
Collaborator Author

I must admit I lost track on what was left to solve... I thought #205 would solve all validation errors, but at the expense of "type-safety" since all "internal" and "external" imports where treated as any.

But in the above example, the error message does not even concern an imported type:

Types of property 'hello' are incompatible.

import { Description } from "./description";

export interface Foo {
  foo: string;
}

export interface Bar {
  hello: Foo; // <<< this is not imported
  desc: Description;
}

@tvillaren
Copy link
Collaborator

Indeed, that's weird as it passed the CI...

Can you try against my latest push (946d7c8)? It does pass on my side, provided you generate the types in the right order (or run yarn gen:all 3 times)

@tvillaren
Copy link
Collaborator

With regards to the issue on "hello" it's a property ordering thing: if you switch hello and description:

export interface Bar {
  hello: Foo; // <<< this is not imported
  desc: Description;
}

you'll have

 Error: 'barSchema' is not compatible with 'Bar':
 ›   Argument of type '{ [x: string]: any; desc?: any; hello?: { foo: string; } | undefined; }' is not assignable to parameter of 
 ›   type 'Bar'.
 ›     Property 'desc' is optional in type '{ [x: string]: any; desc?: any; hello?: { foo: string; } | undefined; }' but required 
 ›   in type 'Bar'.

@schiller-manuel
Copy link
Collaborator Author

What do you mean by switching the properties?

With v.3.7.1 I get the same error:

export interface Bar {
  hello: Foo;
  desc: Description;
}
✖ Validating generated types
 ›   Error: 'barSchema' is not compatible with 'Bar':
 ›   Argument of type '{ [x: string]: any; hello?: { foo: string; } | undefined; desc?: any; }' is not assignable to parameter of type 'Bar'.
 ›     Types of property 'hello' are incompatible.
 ›       Type '{ foo: string; } | undefined' is not assignable to type 'Foo'.
 ›         Type 'undefined' is not assignable to type 'Foo'.

export interface Bar {
  desc: Description;
  hello: Foo;
}
✖ Validating generated types
 ›   Error: 'barSchema' is not compatible with 'Bar':
 ›   Argument of type '{ [x: string]: any; desc?: any; hello?: { foo: string; } | undefined; }' is not assignable to parameter of type 'Bar'.
 ›     Types of property 'hello' are incompatible.
 ›       Type '{ foo: string; } | undefined' is not assignable to type 'Foo'.
 ›         Type 'undefined' is not assignable to type 'Foo'.

tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Feb 27, 2024
@tvillaren
Copy link
Collaborator

This was fixed by #207 and released in 3.7.3

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

No branches or pull requests

2 participants