Skip to content
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

Lookup index in tuple #6226

Closed
OliverJAsh opened this issue Dec 23, 2015 · 2 comments
Closed

Lookup index in tuple #6226

OliverJAsh opened this issue Dec 23, 2015 · 2 comments
Labels
Duplicate An existing issue was already created Question An issue which isn't directly actionable in code

Comments

@OliverJAsh
Copy link
Contributor

const x: [number, string] = [1, 'foo'];
const y = x[1]
y

TypeScript knows y is a string. However, if I ask for x[2], TypeScript tells me it is string | number. Why is this? I expected it not to compile.

@saschanaz
Copy link
Contributor

It seems it needs more discussion. See also #5203.

@DanielRosenwasser
Copy link
Member

This is because a tuple of length n is an anonymous type that has properties 0, ... n - 1, and which effectively extends an array of the union of all element types. Since it's a subtype of Array, it gets an index signature, whose output type is the union of all element types.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Dec 23, 2015
@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Dec 23, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants