Skip to content

Infer fixed length heterogenous arrays as tuples #13883

Closed

Description

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]

Activity

  1. phra commented on Apr 3, 2017

    @phra

    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))
  2. phra commented on Apr 30, 2017

    @phra

    any updates on this?

  3. kitsonk commented on May 1, 2017

    @kitsonk
    Contributor

    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.

  4. jvanbruegge commented on May 1, 2017

    @jvanbruegge
    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).

  5. phra commented on May 1, 2017

    @phra

    see also #15484

  6. mhegazy commented on Feb 8, 2018

    @mhegazy
    Contributor

    Duplicate of #12849

  7. added
    DuplicateAn existing issue was already created
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Feb 8, 2018
  8. typescript-bot commented on Feb 22, 2018

    @typescript-bot
    Contributor

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

  9. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions