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

Compiler does not narrow tuples based on length property #19543

Closed
eyyubovkamran opened this issue Oct 27, 2017 · 6 comments
Closed

Compiler does not narrow tuples based on length property #19543

eyyubovkamran opened this issue Oct 27, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@eyyubovkamran
Copy link

TypeScript Version: 2.7.0-dev.201xxxxx

type A = [string] | [string, number];

let x: A;
if(x.length > 1){
    let y: number = x[1];
}

Expected behavior:
Should compile successfully.

Actual behavior:
error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Oct 27, 2017
@RyanCavanaugh
Copy link
Member

Hijacking this to represent the proposed literal length change proposed at the design meeting today

cc @DanielRosenwasser @ahejlsberg

@mhegazy
Copy link
Contributor

mhegazy commented Oct 28, 2017

related to #19536 and #19463

@DanielRosenwasser DanielRosenwasser changed the title Compiler cannot correctly infer types Compiler does not narrow tuples based on length property Nov 6, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2017

Using > as a discriminant is something we do not see much value in at the time being. but it should work with === assuming we go with proposal in #17765, which will give tuples an explicit length property with a literal type, and hence will allow length to act as a discriminant property.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2017

Should be subsumed by #19463 and #17765.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Nov 6, 2017
@sandersn
Copy link
Member

sandersn commented Nov 7, 2017

Yes, narrowing with === works with #17765.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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
Projects
None yet
Development

No branches or pull requests

5 participants