Investigate (possibly contextual) truthiness narrowing of type parameters #49005
Labels
Experimentation Needed
Someone needs to try this out to see what happens
Fix Available
A PR has been opened for this issue
In Discussion
Not yet reached consensus
Suggestion
An idea for TypeScript
Milestone
In the TypeScript 4.7 timeframe, we introduced stricter behavior in
strictNullChecks
around unconstrained type parameters and empty/weak object types in #48366.However, we but had to revert it back before our release candidate in #48923 because it was so invasive, and it really didn't catch a lot of bugs.
Still, we found a few examples of false-positive cases where users properly narrowed generics, but were encountering the error (#48468).
We wondered if we could provide some degree of narrowing. One experiment was in #48576, where we would only do narrowing in certain contexts on unconstrained type parameters to avoid invasive changes and narrowing time, but it is fairly specific to unconstrained type parameters, and provides poor error messages.
Another idea that @ahejlsberg and @RyanCavanaugh had was to leverage intersection types with
{}
to narrow truthiness checks. At the moment, the ideas involve being more aggressive in subtype/supertype reduction with{}
, so this may introduce surprising changes; however, if we find the results are good, maybe we can take another swing at #48366 down the road.The text was updated successfully, but these errors were encountered: