Skip to content

Type narrowing with literal index in tuples #26323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks done
yahiko00 opened this issue Aug 9, 2018 · 1 comment
Closed
4 tasks done

Type narrowing with literal index in tuples #26323

yahiko00 opened this issue Aug 9, 2018 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@yahiko00
Copy link

yahiko00 commented Aug 9, 2018

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:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@yahiko00 yahiko00 changed the title Type narrowing with litteral index in array Type narrowing with litteral index in tuples Aug 9, 2018
@RyanCavanaugh RyanCavanaugh changed the title Type narrowing with litteral index in tuples Type narrowing with literal index in tuples Aug 9, 2018
@ghost
Copy link

ghost commented Aug 9, 2018

Related: #10530, which had a fix but the performance hit was too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants