Skip to content

Const numbers that index into tuples do not produce the correct type #6678

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
sebdoucet opened this issue Jan 27, 2016 · 7 comments
Closed
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@sebdoucet
Copy link

Hi,

Constants aren't evaluate to access and cast properly union types values:

const ID = 0;
const NAME = 1;

let data: [number, string] = [0, "bob"];

// This need cast to compile
let id: number = data[ID];
let name: string = data[NAME];

Is there a reason why it's not supported ?

Regards,
Sébastien

@RyanCavanaugh
Copy link
Member

We did this for const enums (#3411); I don't see why we wouldn't for this case either

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jan 27, 2016
@sebdoucet
Copy link
Author

It's not compile because you need a cast, it does not recognize ID as 0, so it does not detect you try to get the first entry of the union types, so it does not cast it.

What is important to read is the tuple have first entry as number and second entry as string. Without const, data[0] return a number and data[1] return a string. With const it does not work.

@sandersn sandersn changed the title Union Types and const The value of consts do not index into tuples to give the correct type Jan 27, 2016
@sandersn sandersn changed the title The value of consts do not index into tuples to give the correct type Const numbers do not index into tuples to give the correct type Jan 27, 2016
@sandersn sandersn changed the title Const numbers do not index into tuples to give the correct type Const numbers that index into tuples do not produce the correct type Jan 27, 2016
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". and removed In Discussion Not yet reached consensus labels Feb 1, 2016
@RyanCavanaugh
Copy link
Member

Accepting PRs.

See also linked issues #6804 and #6805

@DrSammyD
Copy link

DrSammyD commented Apr 1, 2016

class x{
   public y(){ return "string" }
}
var z= new x;
const y = "y";
var w = z["y"]();
var v = z[y]();
w.length; //Type safe
v.length; //No longer type safe

Should this be changed too?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 21, 2016

Should this be changed too?

i do not see why not.

@zpdDG4gta8XKpMCd
Copy link

got bitten by it, please fix asap:

nightly build Oct 24, 2016

const values: [number, string] = [1, 'a'];

const one = values[0]; // number, OK

const index = 0; // index is `0` (not just a number)
const another = values[index]; // expected number, actual: string | number <-- WHY?

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@JoshuaKGoldberg
Copy link
Contributor

@RyanCavanaugh this seems to be fixed when checking TypeScript 3.3.3. id and name from the OP are correctly number and string with and without their explicit type annotations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants