myzod icon indicating copy to clipboard operation
myzod copied to clipboard

Fails to compile with TS 4.7.2

Open DavidRigglemanININ opened this issue 2 years ago • 11 comments

After upgrading typescript, I get the following compilation error

node_modules/myzod/libs/types.d.ts:256:40 - error TS2577: Return type annotation circularly references itself.

256     and<K extends AnyType>(schema: K): IntersectionResult<this, K>;
                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/myzod/libs/types.d.ts:306:40 - error TS2577: Return type annotation circularly references itself.

306     and<K extends AnyType>(schema: K): IntersectionResult<this, K>;

DavidRigglemanININ avatar May 24 '22 19:05 DavidRigglemanININ

I'm getting errors on Typescript 4.7.2 as well, but slightly different ones:

src/types.ts:58:22 - error TS2589: Type instantiation is excessively deep and possibly infinite.

58 export type Result = Infer<typeof resultSchema>;
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~

src/types.ts:58:22 - error TS2590: Expression produces a union type that is too complex to represent.

58 export type Result = Infer<typeof resultSchema>;
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~

dabarrell avatar May 26 '22 09:05 dabarrell

I will try and investigate in the next week or so. Hopefully it will be simple enough to fix but when when tsc changes their depth heuristics it can be challenging.

davidmdm avatar May 28 '22 21:05 davidmdm

Any news on this?

lkj4 avatar Jun 06 '22 02:06 lkj4

Using typescript 4.7.4 the error disappear when I set strict: true in tsconfig.json. Cant find any reason why.

thomasskk avatar Jun 23 '22 21:06 thomasskk

I get the same errors with TS 4.7.4 with strict: true. Line 256 and 306. Node v14.19.0.

esadalez avatar Jun 27 '22 12:06 esadalez

Any updates on this? TS 4.8.X is now out.

DavidRigglemanININ avatar Aug 30 '22 16:08 DavidRigglemanININ

Doesn't seem to fix the problem unfortunately. Typescript has changed its heuristics such that the Intersection type is now deemed to circularly reference itself.

Unfortunately I do not foresee time to play around and fix this, so it will have to come from the community if at all, but it is not an easy problem to solve...

davidmdm avatar Sep 03 '22 03:09 davidmdm

Is it worth maybe filing a ticket against the Typescript repo to see if it was a bug on their side in the heuristics algorithm that they'd be willing to address?

DavidRigglemanININ avatar Sep 03 '22 16:09 DavidRigglemanININ

Looks like this issue should be resolved when TypeScript 4.9 releases

DavidRigglemanININ avatar Oct 13 '22 19:10 DavidRigglemanININ

@DavidRigglemanININ Thanks for following this closely. Can you link to what makes you suspect TS4.9 will fix this?

davidmdm avatar Oct 19 '22 00:10 davidmdm

@DavidRigglemanININ Thanks for following this closely. Can you link to what makes you suspect TS4.9 will fix this?

I had created an issue with the TypeScript project and fortunately someone else was able to create a sample project that showed a similar typing regression that started in the same version that I reported. And if you look at the linked issue above, a fix was made and it appears targeted by 4.9

DavidRigglemanININ avatar Oct 24 '22 12:10 DavidRigglemanININ

Unfortunately, while 4.9.4 does fix the originally reported error, now I'm getting a new error:

node_modules/myzod/libs/types.d.ts:51:136 - error TS2589: Type instantiation is excessively deep and possibly infinite.

51 export declare type IntersectionResult<T extends AnyType, K extends AnyType> = T extends ObjectType<any> ? K extends ObjectType<any> ? ObjectIntersection<T, K> : IntersectionType<T, K> : T extends ArrayType<any> ? K extends ArrayType<any> ? ArrayIntersection<T, K> : IntersectionType<T, K> : T extends TupleType<any> ? K extends TupleType<any> ? TupleIntersection<T, K> : IntersectionType<T, K> : T extends MTypeClass<any, any> ? never : K extends MTypeClass<any, any> ? never : IntersectionType<T, K>;

I've updated the ticket in the TypeScript project and will report back if I get any helpful info

DavidRigglemanININ avatar Dec 14 '22 21:12 DavidRigglemanININ

I've create a merge pull request (https://github.com/davidmdm/myzod/pull/58). Could you check this please ?

Nicolas469 avatar Feb 23 '23 14:02 Nicolas469

@Nicolas469 It has been merged!

@DavidRigglemanININ, please confirm that updated to version 1.9.0 fixes your issues and I will close this issue

davidmdm avatar Feb 28 '23 06:02 davidmdm

Sadly no. I'll make another one soon.

Nicolas469 avatar Feb 28 '23 11:02 Nicolas469

@Nicolas469 I am not seeing any errors on my end compiling and running tests, nor playing around with it. What are you experiencing? I have also updated typescript to latest.

davidmdm avatar Feb 28 '23 15:02 davidmdm

I've got an express backend using this as depedency. When running in dev mode (with ts-node-dev) no problem. But if I run tsc to compile the code into javascript, I've got:

#11 12.48 node_modules/myzod/libs/types.d.ts(48,159): error TS2589: Type instantiation is excessively deep and possibly infinite. #11 12.48 node_modules/myzod/libs/types.d.ts(51,128): error TS2589: Type instantiation is excessively deep and possibly infinite.

Nicolas469 avatar Feb 28 '23 15:02 Nicolas469

@Nicolas469 What version of typescript are you using? I am trying to reproduce but it works fine for me using typescript 4.9.5

davidmdm avatar Feb 28 '23 16:02 davidmdm

Also version 4.9.5. The build is working but if you go in the libs folder and check the types.d.ts file, you'll see the errors on lines 48 and 51.

Nicolas469 avatar Feb 28 '23 16:02 Nicolas469

It still fails for me on 4.9.5

node_modules/myzod/libs/types.d.ts:48:159 - error TS2589: Type instantiation is excessively deep and possibly infinite.

48 type ObjectIntersection<O1 extends ObjectType, O2 extends ObjectType> = O1 extends ObjectType<infer Shape1> ? O2 extends ObjectType<infer Shape2> ? ObjectType<Flat<MergeShapes<Shape1, Shape2>>> : never : never; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/myzod/libs/types.d.ts:51:128 - error TS2589: Type instantiation is excessively deep and possibly infinite.

51 export type IntersectionResult<T extends AnyType, K extends AnyType> = T extends ObjectType ? K extends ObjectType ? ObjectIntersection<T, K> : IntersectionType<T, K> : T extends ArrayType ? K extends ArrayType ? ArrayIntersection<T, K> : IntersectionType<T, K> : T extends TupleType ? K extends TupleType ? TupleIntersection<T, K> : IntersectionType<T, K> : T extends MTypeClass<any, any> ? never : K extends MTypeClass<any, any> ? never : IntersectionType<T, K>; ~~~~~~~~~~~~~~~~~~~~~~~~

DavidRigglemanININ avatar Feb 28 '23 17:02 DavidRigglemanININ

@DavidRigglemanININ please check my last pull request.

Nicolas469 avatar Feb 28 '23 17:02 Nicolas469

What's the updated library version to use?

DavidRigglemanININ avatar Feb 28 '23 17:02 DavidRigglemanININ

@DavidRigglemanININ and @Nicolas469 try version 1.9.2 I seem to be having success... If you two can replicate it this issue is closed and myzod is unblocked for new typescript versions!

davidmdm avatar Feb 28 '23 17:02 davidmdm

Looks ok for me :)

Nicolas469 avatar Feb 28 '23 18:02 Nicolas469

Its working for me now. Thanks!

DavidRigglemanININ avatar Feb 28 '23 18:02 DavidRigglemanININ

Closing for now!

Tremendous shoutout to @Nicolas469 for resolving this issue. I have not had the time to work on this for the past year so this is incredible.

🎉 🎉 🎉

davidmdm avatar Feb 28 '23 19:02 davidmdm

Are there any implications to this change? In other words, what are we losing? Will some consumers of this library now no longer get type checking, leading to potentially invalid types being used but not checked? Or is this strictly internal type checking within the library. It might not be a bad idea to revisit every once in a while with new TypeScript releases if the ignores can be removed.

DavidRigglemanININ avatar Feb 28 '23 19:02 DavidRigglemanININ

I am seeing this error with myzod v1.11.0, Typescript v5.5.4, and Node v22.4.1.

node_modules/.pnpm/[email protected]/node_modules/myzod/libs/types.d.ts:49:146 - error TS2589: Type instantiation is excessively deep and possibly infinite.

49 type ArrayIntersection<A1 extends ArrayType<any>, A2 extends ArrayType<any>> = A1 extends ArrayType<infer S1> ? A2 extends ArrayType<infer S2> ? ArrayType<IntersectionResult<S1, S2>> : never : never;
                                                                                                                                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in node_modules/.pnpm/[email protected]/node_modules/myzod/libs/types.d.ts:49

enthooz avatar Jul 24 '24 18:07 enthooz

This error goes away when I downgrade to Typescript v5.3.3 maintaining same versions of myzod (v1.11.0) and Node (v22.4.1).

enthooz avatar Jul 24 '24 18:07 enthooz