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

Infer fixed length heterogenous arrays as tuples #13883

Closed
jvanbruegge opened this issue Feb 5, 2017 · 7 comments
Closed

Infer fixed length heterogenous arrays as tuples #13883

jvanbruegge opened this issue Feb 5, 2017 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@jvanbruegge
Copy link

As the use of Heterogenous arrays for other purposes as tuples is minimal, I would change default inferance for fixed length heterogenous arrays to tuples to minimize the needed as statements in the code.

TypeScript Version: 2.1

const f : (s : string) => number = s => parseInt(s);
const arr : string[] = ['0', '1', '2'];

const res = arr
    .map(s => [s, f(s)]) //Should be infered as [string, number] not [string | number]
    .map(([s, n]) => f(s) + n); //Error: s has type number | string

Expected behavior:
the tuple has the infered type [string, number]

Actual behavior:
the tuple is typed [string | number]

@phra
Copy link

phra commented Apr 3, 2017

yeah, it would be nice. i just experienced the same issue.

Typescript version: 2.2.x

UNION TYPE INFERRED:

function* CounterGenerator(): IterableIterator<number> {
    let i = 1
    while (1) {
        yield i++
    }
}

function* StringGenerator(): IterableIterator<string> {
    let i = 1
    while (1) {
        yield String(i++)
    }
}

function getTuple()/*: [number, string]*/ { // return type stripped
    return [CounterGenerator().next().value, StringGenerator().next().value]
} 

const [x, y] = getTuple()

console.log(x + parseInt(y)) // tsc complains about y

NO ERRORS:

function* CounterGenerator(): IterableIterator<number> {
    let i = 1
    while (1) {
        yield i++
    }
}

function* StringGenerator(): IterableIterator<string> {
    let i = 1
    while (1) {
        yield String(i++)
    }
}

function getTuple(): [number, string] {
    return [CounterGenerator().next().value, StringGenerator().next().value]
} 

const [x, y] = getTuple()

console.log(x + parseInt(y))

@phra
Copy link

phra commented Apr 30, 2017

any updates on this?

@kitsonk
Copy link
Contributor

kitsonk commented May 1, 2017

It is really unsafe to infer tuple types... There is a lot of discussion on this in other issues. It would and could break a lot of other code where someone is using an array that they don't intend to be a tuple. You have to choose an order of precedence of assign types when they overlap, and choosing an array is far more logical and common than a tuple type. Just because an array literal is a const and is heterogeneous doesn't mean that it is intended to be a tuple. Especially because even with tuple types, all the run-time array functionality is available. It makes a lot of sense that tuples need to be explicitly declared as it demonstrates the intent of ambiguous code.

@jvanbruegge
Copy link
Author

what is the use case of an heterogenous array that is not used as tuple? And it may be breaking, but I would assume only for a small amount of people and typescript had breaking changes before (2.1 -> 2.2).

@phra
Copy link

phra commented May 1, 2017

see also #15484

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Feb 8, 2018

Duplicate of #12849

@mhegazy mhegazy marked this as a duplicate of #12849 Feb 8, 2018
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 8, 2018
@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 Jul 3, 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

6 participants