Search Terms
type inference narrowing literal array tuple
Suggestion
It would be convenient if TypeScript could narrow the type of an item in a tuple, with literal numeric index, especially when the tuple can contain null items.
Use Cases
For fixed length tuples, we often access each item with a literal numeric index.
When an item of such a tuple is null, TypeScript seems not to be able to narrow the item's type after a null check.
Examples
let period: [Date, Date | null] = [new Date(), null];
let dateEndStr = [period[1] ? period[1].toISOString() : null]; // Error: object period can be null
Checklist
My suggestion meets these guidelines:
Search Terms
type inference narrowing literal array tuple
Suggestion
It would be convenient if TypeScript could narrow the type of an item in a tuple, with literal numeric index, especially when the tuple can contain null items.
Use Cases
For fixed length tuples, we often access each item with a literal numeric index.
When an item of such a tuple is null, TypeScript seems not to be able to narrow the item's type after a null check.
Examples
Checklist
My suggestion meets these guidelines: