-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Nested mapped types are inferred to be any #23918
Comments
The compiler is actually inferring the right type, but when producing the text representation of the type (e.g. for tool tips or declaration file emit) it stops after observing multiple levels of nesting of the same type in order to avoid what might be an infinitely nested type. It is a known design limitation. See also #23433. |
I see! Thank you for the quick and enlightening response! Do you have any ideas for workarounds to achieve the desired .d.ts output? In my real application I would really like to be able to export these types across package boundaries. Would it be possible to work around the issue by adding some "uniqueness" at each level? What exactly triggers the suspicion that the type is infinitely recursive? Is it sufficient to encounter any parametrization of Mapper in the above example, or would it help to add another type parameter that's different at each level? (Not really sure how to achieve that, in any case...) Alternatively, can we improve the type-to-text algorithm to "tolerate" potential recursion until it reaches a certain maximum depth? If you point me at the relevant code, I would be very willing to give it a shot. |
Another thought/question. If the compiler succeeds in inferring a type from an initializer, will it then be definitely possible to display the type without endless recursion? If so, could we add a flag to the type to remember that it's safe to display? Or does "possible to infer" not imply "possible to display"? I feel like I might be missing some distinction here. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Is there a workaround to help the text representation be useful then? |
This is NOT just text representation. It really allows you to set anything in that type without throwing any kind of errors. Very problematic and unsafe. |
I'm trying to have the compiler infer the type of a deeply nested object, while transforming the type with a mapped type at every level of nesting. But the inferred type seems to be resolved to any at a certain depth.
TypeScript Version: 2.9.0-dev.20180503
Search Terms:
Infer
Nested
Mapped type
Any
Code
Expected behavior:
I want the compiler to be able to infer a deeply nested mapped type.
Actual behavior:
The inferred type seems to be resolved to any at a certain depth. Sometimes, the VS code tooltips gives the correct type while the emitted .d.ts file does not.
Playground Link: link
Related Issues: Possibly #22715
The text was updated successfully, but these errors were encountered: