-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Tuple conformance #822
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
Merged
Merged
Tuple conformance #822
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
tests/baselines/reference/assignmentCompatBetweenTupleAndArray.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatBetweenTupleAndArray.ts(17,1): error TS2322: 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'. | ||
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatBetweenTupleAndArray.ts(18,1): error TS2322: Type '{}[]' is not assignable to type '[{}]': | ||
Property '0' is missing in type '{}[]'. | ||
|
||
|
||
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatBetweenTupleAndArray.ts (2 errors) ==== | ||
var numStrTuple: [number, string]; | ||
var numNumTuple: [number, number]; | ||
var numEmptyObjTuple: [number, {}]; | ||
var emptyObjTuple: [{}]; | ||
|
||
var numArray: number[]; | ||
var emptyObjArray: {}[]; | ||
|
||
// no error | ||
numArray = numNumTuple; | ||
emptyObjArray = emptyObjTuple; | ||
emptyObjArray = numStrTuple; | ||
emptyObjArray = numNumTuple; | ||
emptyObjArray = numEmptyObjTuple; | ||
|
||
// error | ||
numArray = numStrTuple; | ||
~~~~~~~~ | ||
!!! error TS2322: Type '[number, string]' is not assignable to type 'number[]': | ||
!!! error TS2322: Types of property 'pop' are incompatible: | ||
!!! error TS2322: Type '() => {}' is not assignable to type '() => number': | ||
!!! error TS2322: Type '{}' is not assignable to type 'number'. | ||
emptyObjTuple = emptyObjArray; | ||
~~~~~~~~~~~~~ | ||
!!! error TS2322: Type '{}[]' is not assignable to type '[{}]': | ||
!!! error TS2322: Property '0' is missing in type '{}[]'. | ||
|
37 changes: 37 additions & 0 deletions
37
tests/baselines/reference/assignmentCompatBetweenTupleAndArray.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//// [assignmentCompatBetweenTupleAndArray.ts] | ||
var numStrTuple: [number, string]; | ||
var numNumTuple: [number, number]; | ||
var numEmptyObjTuple: [number, {}]; | ||
var emptyObjTuple: [{}]; | ||
|
||
var numArray: number[]; | ||
var emptyObjArray: {}[]; | ||
|
||
// no error | ||
numArray = numNumTuple; | ||
emptyObjArray = emptyObjTuple; | ||
emptyObjArray = numStrTuple; | ||
emptyObjArray = numNumTuple; | ||
emptyObjArray = numEmptyObjTuple; | ||
|
||
// error | ||
numArray = numStrTuple; | ||
emptyObjTuple = emptyObjArray; | ||
|
||
|
||
//// [assignmentCompatBetweenTupleAndArray.js] | ||
var numStrTuple; | ||
var numNumTuple; | ||
var numEmptyObjTuple; | ||
var emptyObjTuple; | ||
var numArray; | ||
var emptyObjArray; | ||
// no error | ||
numArray = numNumTuple; | ||
emptyObjArray = emptyObjTuple; | ||
emptyObjArray = numStrTuple; | ||
emptyObjArray = numNumTuple; | ||
emptyObjArray = numEmptyObjTuple; | ||
// error | ||
numArray = numStrTuple; | ||
emptyObjTuple = emptyObjArray; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//// [bestCommonTypeOfTuple.ts] | ||
function f1(x: number): string { return "foo"; } | ||
|
||
function f2(x: number): number { return 10; } | ||
|
||
function f3(x: number): boolean { return true; } | ||
|
||
enum E1 { one } | ||
|
||
enum E2 { two } | ||
|
||
|
||
var t1: [(x: number) => string, (x: number) => number]; | ||
var t2: [E1, E2]; | ||
var t3: [number, any]; | ||
var t4: [E1, E2, number]; | ||
|
||
// no error | ||
t1 = [f1, f2]; | ||
t2 = [E1.one, E2.two]; | ||
t3 = [5, undefined]; | ||
t4 = [E1.one, E2.two, 20]; | ||
var e1 = t1[2]; // {} | ||
var e2 = t2[2]; // {} | ||
var e3 = t3[2]; // any | ||
var e4 = t4[3]; // number | ||
|
||
//// [bestCommonTypeOfTuple.js] | ||
function f1(x) { | ||
return "foo"; | ||
} | ||
function f2(x) { | ||
return 10; | ||
} | ||
function f3(x) { | ||
return true; | ||
} | ||
var E1; | ||
(function (E1) { | ||
E1[E1["one"] = 0] = "one"; | ||
})(E1 || (E1 = {})); | ||
var E2; | ||
(function (E2) { | ||
E2[E2["two"] = 0] = "two"; | ||
})(E2 || (E2 = {})); | ||
var t1; | ||
var t2; | ||
var t3; | ||
var t4; | ||
// no error | ||
t1 = [f1, f2]; | ||
t2 = [0 /* one */, 0 /* two */]; | ||
t3 = [5, undefined]; | ||
t4 = [0 /* one */, 0 /* two */, 20]; | ||
var e1 = t1[2]; // {} | ||
var e2 = t2[2]; // {} | ||
var e3 = t3[2]; // any | ||
var e4 = t4[3]; // number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
=== tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts === | ||
function f1(x: number): string { return "foo"; } | ||
>f1 : (x: number) => string | ||
>x : number | ||
|
||
function f2(x: number): number { return 10; } | ||
>f2 : (x: number) => number | ||
>x : number | ||
|
||
function f3(x: number): boolean { return true; } | ||
>f3 : (x: number) => boolean | ||
>x : number | ||
|
||
enum E1 { one } | ||
>E1 : E1 | ||
>one : E1 | ||
|
||
enum E2 { two } | ||
>E2 : E2 | ||
>two : E2 | ||
|
||
|
||
var t1: [(x: number) => string, (x: number) => number]; | ||
>t1 : [(x: number) => string, (x: number) => number] | ||
>x : number | ||
>x : number | ||
|
||
var t2: [E1, E2]; | ||
>t2 : [E1, E2] | ||
>E1 : E1 | ||
>E2 : E2 | ||
|
||
var t3: [number, any]; | ||
>t3 : [number, any] | ||
|
||
var t4: [E1, E2, number]; | ||
>t4 : [E1, E2, number] | ||
>E1 : E1 | ||
>E2 : E2 | ||
|
||
// no error | ||
t1 = [f1, f2]; | ||
>t1 = [f1, f2] : [(x: number) => string, (x: number) => number] | ||
>t1 : [(x: number) => string, (x: number) => number] | ||
>[f1, f2] : [(x: number) => string, (x: number) => number] | ||
>f1 : (x: number) => string | ||
>f2 : (x: number) => number | ||
|
||
t2 = [E1.one, E2.two]; | ||
>t2 = [E1.one, E2.two] : [E1, E2] | ||
>t2 : [E1, E2] | ||
>[E1.one, E2.two] : [E1, E2] | ||
>E1.one : E1 | ||
>E1 : typeof E1 | ||
>one : E1 | ||
>E2.two : E2 | ||
>E2 : typeof E2 | ||
>two : E2 | ||
|
||
t3 = [5, undefined]; | ||
>t3 = [5, undefined] : [number, undefined] | ||
>t3 : [number, any] | ||
>[5, undefined] : [number, undefined] | ||
>undefined : undefined | ||
|
||
t4 = [E1.one, E2.two, 20]; | ||
>t4 = [E1.one, E2.two, 20] : [E1, E2, number] | ||
>t4 : [E1, E2, number] | ||
>[E1.one, E2.two, 20] : [E1, E2, number] | ||
>E1.one : E1 | ||
>E1 : typeof E1 | ||
>one : E1 | ||
>E2.two : E2 | ||
>E2 : typeof E2 | ||
>two : E2 | ||
|
||
var e1 = t1[2]; // {} | ||
>e1 : {} | ||
>t1[2] : {} | ||
>t1 : [(x: number) => string, (x: number) => number] | ||
|
||
var e2 = t2[2]; // {} | ||
>e2 : {} | ||
>t2[2] : {} | ||
>t2 : [E1, E2] | ||
|
||
var e3 = t3[2]; // any | ||
>e3 : any | ||
>t3[2] : any | ||
>t3 : [number, any] | ||
|
||
var e4 = t4[3]; // number | ||
>e4 : number | ||
>t4[3] : number | ||
>t4 : [E1, E2, number] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//// [bestCommonTypeOfTuple2.ts] | ||
interface base { } | ||
interface base1 { i } | ||
class C implements base { c } | ||
class D implements base { d } | ||
class E implements base { e } | ||
class F extends C { f } | ||
|
||
class C1 implements base1 { i = "foo"; c } | ||
class D1 extends C1 { i = "bar"; d } | ||
|
||
var t1: [C, base]; | ||
var t2: [C, D]; | ||
var t3: [C1, D1]; | ||
var t4: [base1, C1]; | ||
var t5: [C1, F] | ||
|
||
var e11 = t1[4]; // base | ||
var e21 = t2[4]; // {} | ||
var e31 = t3[4]; // C1 | ||
var e41 = t4[2]; // base1 | ||
var e51 = t5[2]; // {} | ||
|
||
|
||
//// [bestCommonTypeOfTuple2.js] | ||
var __extends = this.__extends || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
__.prototype = b.prototype; | ||
d.prototype = new __(); | ||
}; | ||
var C = (function () { | ||
function C() { | ||
} | ||
return C; | ||
})(); | ||
var D = (function () { | ||
function D() { | ||
} | ||
return D; | ||
})(); | ||
var E = (function () { | ||
function E() { | ||
} | ||
return E; | ||
})(); | ||
var F = (function (_super) { | ||
__extends(F, _super); | ||
function F() { | ||
_super.apply(this, arguments); | ||
} | ||
return F; | ||
})(C); | ||
var C1 = (function () { | ||
function C1() { | ||
this.i = "foo"; | ||
} | ||
return C1; | ||
})(); | ||
var D1 = (function (_super) { | ||
__extends(D1, _super); | ||
function D1() { | ||
_super.apply(this, arguments); | ||
this.i = "bar"; | ||
} | ||
return D1; | ||
})(C1); | ||
var t1; | ||
var t2; | ||
var t3; | ||
var t4; | ||
var t5; | ||
var e11 = t1[4]; // base | ||
var e21 = t2[4]; // {} | ||
var e31 = t3[4]; // C1 | ||
var e41 = t4[2]; // base1 | ||
var e51 = t5[2]; // {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
heyYui, can you file a bug on having us provide a better error message here. Here we're giving an error about 'pop' being wrong (where pop is a method on Array), where it would probably be better to indicate that the type of the tuple array isn't assignable to number.
This is all about improving the error message and providing more directed, useful, information to the person who runs into this.
Thanks!