Skip to content

Poor tuple support #20899

Closed
Closed

Description

TypeScript Version: 2.7.0-dev.20171226

Code

function getPromise(): Promise<[string, number]> {
	return Promise.resolve(['', 0]);
}

The above is an example, but it's not the only example of where TypeScript has trouble with tuples.

Others include:

let a: [string, number] = ['', 0];
let b: [string, number] = a.slice();

The above one is harder, but it'd be great if array.slice() and array.slice(0) could be used to clone tuples.

https://github.com/Zarel/Pokemon-Showdown/blob/2b5654e307d7d65ee16c8bf55961b352c97c96e9/users.js#L1438

The above linked example disappeared when I tried to construct a simple equivalent testcase:

let a = null as [string, string, object][] | null;

if (a) {
	a.push(['', '', {}]);
} else {
	a = [['', '', {}]];
}

But if you care to clone that repo, checkout that commit, and delete the // @ts-ignore, you'll get a similar error message as the others, where the tuple degrades to an array.

Expected behavior:

No errors

Actual behavior:

test1.ts(2,2): error TS2322: Type 'Promise<(string | number)[]>' is not assignable to type 'Promise<[string, number]>'.
  Type '(string | number)[]' is not assignable to type '[string, number]'.
    Property '0' is missing in type '(string | number)[]'.
test1.ts(2,5): error TS2322: Type '(string | number)[]' is not assignable to type '[string, number]'.
  Property '0' is missing in type '(string | number)[]'.
users.js(1438,4): error TS2322: Type '(string | Connection)[][]' is not assignable to type '[string, string, Connection][] | null'.
  Type '(string | Connection)[][]' is not assignable to type '[string, string, Connection][]'.
    Type '(string | Connection)[]' is not assignable to type '[string, string, Connection]'.
      Property '0' is missing in type '(string | Connection)[]'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions