-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Adding support for tuple types (e.g. [number, string]) #428
Changes from all commits
5b25524
3b1dbad
ef52312
92b3677
f0b33b3
0cf503f
c0e802d
63b83e7
be7e0a7
b4cddc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
==== tests/cases/compiler/tupleTypes.ts (9 errors) ==== | ||
var v1: []; // Error | ||
~~ | ||
!!! A tuple type element list cannot be empty. | ||
var v2: [number]; | ||
var v3: [number, string]; | ||
var v4: [number, [string, string]]; | ||
|
||
var t: [number, string]; | ||
var t0 = t[0]; // number | ||
var t0: number; | ||
var t1 = t[1]; // string | ||
var t1: string; | ||
var t2 = t[2]; // {} | ||
var t2: {}; | ||
|
||
t = []; // Error | ||
~ | ||
!!! Type '{}[]' is not assignable to type '[number, string]': | ||
!!! Property '0' is missing in type '{}[]'. | ||
t = [1]; // Error | ||
~ | ||
!!! Type '[number]' is not assignable to type '[number, string]': | ||
!!! Property '1' is missing in type '[number]'. | ||
t = [1, "hello"]; // Ok | ||
t = ["hello", 1]; // Error | ||
~ | ||
!!! Type '[string, number]' is not assignable to type '[number, string]': | ||
!!! Types of property '0' are incompatible: | ||
!!! Type 'string' is not assignable to type 'number'. | ||
t = [1, "hello", 2]; // Ok | ||
|
||
var tf: [string, (x: string) => number] = ["hello", x => x.length]; | ||
|
||
declare function ff<T, U>(a: T, b: [T, (x: T) => U]): U; | ||
var ff1 = ff("hello", ["foo", x => x.length]); | ||
var ff1: number; | ||
|
||
function tuple2<T0, T1>(item0: T0, item1: T1): [T0, T1]{ | ||
return [item0, item1]; | ||
} | ||
|
||
var tt = tuple2(1, "string"); | ||
var tt0 = tt[0]; | ||
var tt0: number; | ||
var tt1 = tt[1]; | ||
var tt1: string; | ||
var tt2 = tt[2]; | ||
var tt2: {}; | ||
|
||
tt = tuple2(1, undefined); | ||
tt = [1, undefined]; | ||
tt = [undefined, undefined]; | ||
tt = []; // Error | ||
~~ | ||
!!! Type '{}[]' is not assignable to type '[number, string]'. | ||
|
||
var a: number[]; | ||
var a1: [number, string]; | ||
var a2: [number, number]; | ||
var a3: [number, {}]; | ||
a = a1; // Error | ||
~ | ||
!!! Type '[number, string]' is not assignable to type 'number[]': | ||
!!! Types of property 'pop' are incompatible: | ||
!!! Type '() => {}' is not assignable to type '() => number': | ||
!!! Type '{}' is not assignable to type 'number'. | ||
a = a2; | ||
a = a3; // Error | ||
~ | ||
!!! Type '[number, {}]' is not assignable to type 'number[]': | ||
!!! Types of property 'pop' are incompatible: | ||
!!! Type '() => {}' is not assignable to type '() => number': | ||
!!! Type '{}' is not assignable to type 'number'. | ||
a1 = a2; // Error | ||
~~ | ||
!!! Type '[number, number]' is not assignable to type '[number, string]': | ||
!!! Types of property '1' are incompatible: | ||
!!! Type 'number' is not assignable to type 'string'. | ||
a1 = a3; // Error | ||
~~ | ||
!!! Type '[number, {}]' is not assignable to type '[number, string]': | ||
!!! Types of property '1' are incompatible: | ||
!!! Type '{}' is not assignable to type 'string'. | ||
a3 = a1; | ||
a3 = a2; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var v1: []; // Error | ||
var v2: [number]; | ||
var v3: [number, string]; | ||
var v4: [number, [string, string]]; | ||
|
||
var t: [number, string]; | ||
var t0 = t[0]; // number | ||
var t0: number; | ||
var t1 = t[1]; // string | ||
var t1: string; | ||
var t2 = t[2]; // {} | ||
var t2: {}; | ||
|
||
t = []; // Error | ||
t = [1]; // Error | ||
t = [1, "hello"]; // Ok | ||
t = ["hello", 1]; // Error | ||
t = [1, "hello", 2]; // Ok | ||
|
||
var tf: [string, (x: string) => number] = ["hello", x => x.length]; | ||
|
||
declare function ff<T, U>(a: T, b: [T, (x: T) => U]): U; | ||
var ff1 = ff("hello", ["foo", x => x.length]); | ||
var ff1: number; | ||
|
||
function tuple2<T0, T1>(item0: T0, item1: T1): [T0, T1]{ | ||
return [item0, item1]; | ||
} | ||
|
||
var tt = tuple2(1, "string"); | ||
var tt0 = tt[0]; | ||
var tt0: number; | ||
var tt1 = tt[1]; | ||
var tt1: string; | ||
var tt2 = tt[2]; | ||
var tt2: {}; | ||
|
||
tt = tuple2(1, undefined); | ||
tt = [1, undefined]; | ||
tt = [undefined, undefined]; | ||
tt = []; // Error | ||
|
||
var a: number[]; | ||
var a1: [number, string]; | ||
var a2: [number, number]; | ||
var a3: [number, {}]; | ||
a = a1; // Error | ||
a = a2; | ||
a = a3; // Error | ||
a1 = a2; // Error | ||
a1 = a3; // Error | ||
a3 = a1; | ||
a3 = a2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be worth breaking this up into a few different files by concept (ex tupleTypeInference, etc). We also need a lot more coverage here. We should have tests for things like var x = 0;
var y = 3;
var tt = tuple2(1, "a");
var r1 = tt[x];
var r2 = tt[y]; Tuples crossing module boundaries, usages in class hierarchies with array types, assignability with nested tuples and nested arrays, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After you merge the changes from typeWriter (now in master), be sure to modify SyntaxKind.LastTypeNode accordingly.