Alias type conditions to desctrutured variables #46143
Labels
Fix Available
A PR has been opened for this issue
In Discussion
Not yet reached consensus
Suggestion
An idea for TypeScript
Milestone
Suggestion
π Search Terms
is:issue is:open control flow analysis destructure
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
TypeScript 4.4 has a feature called Control Flow Analysis of Aliased Conditions and Discriminants, which essentially allows a variable to carry type information about another variable.
I believe something similar could happen when destructuring discriminated unions.
Object destructuring is a very common pattern in JavaScript, but we need to avoid it often in TypeScript because it loses type information:
has to be artificially transformed to satisfy the typechecker:
With TypeScript 4.4, it looks like we're very close to having this feature:
π Motivating Example
These patterns emerge naturally in JavaScript and currently have to be transformed to fit TypeScript's ability to infer types.
The IteratorResult example above.
Another example is with action to state reducers:
π» Use Cases
I think the examples above already cover this.
The text was updated successfully, but these errors were encountered: