Closed
Description
openedon Dec 26, 2017
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.
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