You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Alias type conditions to desctrutured variablesΒ #46143
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:
declarevarit: Iterator<number>;const{ value, done }=it.next();if(!done){value;// we know it's a number, but it's any}
has to be artificially transformed to satisfy the typechecker:
constresult=it.next();if(!result.done){result.value;// is narrowed to a number}
With TypeScript 4.4, it looks like we're very close to having this feature:
constresult=it.next();const{ value, done }=result;if(!done){result.value;// `done` tracks result's typevalue;// but not value's}
π 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:
Bruce Pascoe (@fatcerberus), but maybe not as ambitious.
Anyway, I'm aware this should've been requested/reported multiple times.
But because TS4.4 brought a feature somewhat related to this, I thought I could use the opportunity to bring this up.
This case was explicitly mentioned in PR that implemented aliases #44730 (at the end, before list if fixed issues)
However not sure if there is big difference with #35873 or if there is better issue tracking correlation during destructuring only.
Activity
fatcerberus commentedon Sep 30, 2021
This looks similar in spirit to #35873
ilogico commentedon Sep 30, 2021
Bruce Pascoe (@fatcerberus), but maybe not as ambitious.
Anyway, I'm aware this should've been requested/reported multiple times.
But because TS4.4 brought a feature somewhat related to this, I thought I could use the opportunity to bring this up.
IllusionMH commentedon Sep 30, 2021
This case was explicitly mentioned in PR that implemented aliases #44730 (at the end, before list if fixed issues)
However not sure if there is big difference with #35873 or if there is better issue tracking correlation during destructuring only.