Cascade type narrowing to child constants #53202
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
π Search Terms
narrow child variable related
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Constants that were assigned a child value of another variable or constant should have their type narrowed alongside their parent.
π Motivating Example
Consider the following code:
In
someFunction
, TypeScript keeps the type ofvalue
as it was originally assigned (string | number | Date
).What if dev expects
value
to have been narrowed tostring
?This already happens with the root keys of an object
This is observable in both examples.
π» Use Cases
Use case: Working with discriminated unions holding nested objects and arrays
To achieve such narrowing, one currently must adjust coding style. ie:
The text was updated successfully, but these errors were encountered: