-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Broken inferred recursive types #43817
Comments
So, the declaration emit error is very intentional, because the declaration emit that is possible for us is imprecise: see those |
Below is a version with an explicit type annotation. This works as expected and generates a nice and compact declaration file. type Builder<T> = {
withComment: (comment: string) => Builder<T & { comment: string }>,
withSpecialComment: () => Builder<T & { comment: string }>,
withTrackingNumber: (tracking: number) => Builder<T & { tracking: number }>,
build: () => T
};
const Builder: <T extends {}>(model: T) => Builder<T> = <T extends {}>(model: T) => ({
withComment: (comment: string) => Builder({ ...model, comment }),
withSpecialComment: () => Builder(model).withComment('Special comment'),
withTrackingNumber: (tracking: number) => Builder({ ...model, tracking }),
build: () => model });
const a = Builder({}); |
Currently running into this issue. I find that everything is fine until this error comes up, then suddenly the entire language server starts taking 3-6 seconds to respond to anything. Unfortunately, It's very, very much impossible for me to give "An explicit type annotation is needed." Also, everything was working fine this morning, and I can't seem to figure out what has changed since then. So I know TS can do it. I even have a demo within my installed project that works great, but converting it to an npm package appears to have made the recursion too much somehow. |
I made some progress on this issue by converting all my interfaces to abstract classes. Not sure why it worked, but you can read more here: |
Closing this issue without a fix - means breaking backward compatibility in a minor version. |
i have same issue with mongoose when i try update my document with error text:
|
I haven't shared it in this thread, but one fix I found is to use abstract classes instead of interfaces in certain areas. I have no idea why this works, but an abstract class that references itself is much happier than an interface that does. Worked for me at least... |
This issue just started popping up without any changes made to our project. |
This is happening with AJV schemas (trivial ones at that), but only in my editor. The actual |
Bug Report
TypeScript inference on recursive types is broken since v4.1
π Search Terms
π Version & Regression Information
β― Playground Link
Playground link with 4.3.0-beta - with the TS errors
Playground link with 4.0.5 - no errors
π» Code
π Actual behavior
TS compiler error: "The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.(7056)"
π Expected behavior
No errors on compilation
The text was updated successfully, but these errors were encountered: