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

Incorrect typing when flip-flopping an array of tuples #10094

Closed
dsifford opened this issue Aug 2, 2016 · 2 comments
Closed

Incorrect typing when flip-flopping an array of tuples #10094

dsifford opened this issue Aug 2, 2016 · 2 comments

Comments

@dsifford
Copy link
Contributor

dsifford commented Aug 2, 2016

TypeScript Version: nightly (2.0.0-dev.201xxxxx)

Playground

Code

const x: [string,number][] = [
    ['one', 1],
    ['two', 2],
    ['three', 3]
];

// Type of y should be [number,string][]
// Instead, it is (number|string)[][]
const y = x.map(([a,b]) => [b,a]);

Expected behavior:

This may be an expected feature of the language, and if so, please disregard.

I expected the type of y to be [number,string][]

Actual behavior:

Type of y is (number|string)[][]

@weswigham
Copy link
Member

As a workaround, you can cast your result:

const y = x.map(([a,b]) => ([b,a] as [number, string]));

Or, a personal favorite, write your own 'tuple constructor', which forces a tuple type to be inferred:

function tuple<A, B>(a: A, b: B): [A, B] {
    return [a, b];
}

const y = x.map(([a,b]) => tuple(b, a));

In any case, I think this is a duplicate of #6574, and discussion should probably continue there.

@dsifford
Copy link
Contributor Author

dsifford commented Aug 2, 2016

Whoops! Didn't see the other discussion. Sorry about that.

And yep, I've been using casting. Totally not a giant issue for me, but just wanted to throw it out there.

Love the tuple constructor! Clever!

Thanks again 👍

@dsifford dsifford closed this as completed Aug 2, 2016
@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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants