-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Description
π Search Terms
infer too complicated, infer logic mismatch, infer improvement
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
In an expression such as type Something<T> = T extends Promise<infer U> ? U : never; the occurence of "infer U" will receive the type within the pattern it resides and syntactically suggests that U is the type that is received within that layer (thinking about layer depth as in PromiseIsLayer1<infer UIsLayer2OrTheInnermostLayer>). However, within the conditional it becomes clear that U now refers to the pattern as a whole, checking not only the received type within the (in this case) generic, but also the structure surrounding it.
Therefore I suggest the infer keyword to be used differently and a new keyword to be added (any solution that is a readable and self explanatory syntax would be sufficient)
π Motivating Example
type Something<T> = T extends pattern Promise<infer> U ? U : never;
The keyword "pattern" would ensure the intuitive understanding of the following to be taken as a whole and assign it to the temporary type variable U, making it explicitly a type consisting of that pattern. The infer keyword only holds the type information and places it in the resolved form of U, in that case we could assume Something<Promise<string>> being Something<U>. I see no reason not to omit the variable next to infer, which could receive a variable to store the type information in under the hood.
π» Use Cases
-
What do you want to use this for?
A better developer experience, that is more intuitive and fits the logic the underlying implementation follows. -
What shortcomings exist with current approaches?
It is not intuitive and exhibits logical flaws that have to be overcome to gain the understanding that could be easily there. -
What workarounds are you using in the meantime?
Understanding the concept using more time than necessary. Grasping it isn't the problem, it is the average amount of time spent on it, that holds developers back.