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

Array Type Inference #29163

Closed
cinderblock opened this issue Dec 27, 2018 · 5 comments
Closed

Array Type Inference #29163

cinderblock opened this issue Dec 27, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@cinderblock
Copy link

cinderblock commented Dec 27, 2018

TypeScript Version: 3.3.0-dev.20181222

Search Terms:

  • array
  • type
  • inference
  • index

Code
Playground Link

const typed = ['a', 1] as [string, number];
typed[0].length; // Works
typed[1].toPrecision; // Works
typed[2]; // Errors as expected

const array = ['a', 1];
array[0].length; // Error
array[1].toPrecision; // Error
array[2]; // Should error

Expected behavior:

TypeScript infers type of array as [string, number].

Actual behavior:

TypeScript infers type of array as (string | number)[].

Related Issues:
#25516

@fatcerberus
Copy link

Known issue - array literals are inferred as a union of their member types. I don't think it's considered a bug.

@cinderblock
Copy link
Author

I see. I looked through the FAQ pages of other bugs and did not find exactly equivalent.

I'll let them close this if this is desired behavior.

I'm curious what reasons there are for this not being the desired behavior!

@fatcerberus
Copy link

It's been discussed on the TypeScript gitter several times in the past, and I was pretty sure there was a GitHub issue that described the design challenges at length, but I can't find it now...

See also #27179, #16656.

@weswigham weswigham added the Duplicate An existing issue was already created label Jan 2, 2019
@weswigham
Copy link
Member

Aye, the two links @fatcerberus has - #27179 and #16656 are where this problem is being discussed.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

Successfully merging a pull request may close this issue.

4 participants