Make type narrowing for destructured discriminated unions work for more types #59657
Open
6 tasks done
Labels
Help Wanted
You can do this
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
π Search Terms
discriminant union, ref, control flow guard, type narrowing
β Viability Checklist
β Suggestion
Treat types like
Ref<T>
as a discriminant property in a union or find a way to narrow the type ofpayload
π Motivating Example
This is a very common use case in the Vue Pinia state store library, millions of projects use this library and have code like
If we can improve this type narrowing behavior, the narrowed
payload
type can helps developer write safer code than beforeπ» Use Cases
More detailed playground link
The use cases is actually shown in the motivating example.
I've dig into the
checker.ts
for some time and here's my findingsgetDiscriminantPropertyAccess
cannot treatready
as a discriminant property now because it needs to checkCheckFlags.Discriminant
which impliesCheckFlags.HasLiteralType
. It's a pretty strict check and as its name describes,Ref<T>
has no chance to pass this check.Can we use type predicates or assert function to add more information to
payload
's flow list? If it's possible, maybe we can do following steps while examine thepayload
payload
's symbol, if its declaration is aBindingPattern
payload
, if the narroweddata
is the initializer ofpayload
's declarationpayload
based on the narroweddata
The text was updated successfully, but these errors were encountered: