-
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
Contextually type parameters in super calls using type arguments on the base class. #1816
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93dbcf0
Contextually type parameters in super calls using type arguments on t…
DanielRosenwasser 4646d63
Fixed fourslash test.
DanielRosenwasser 8ee09db
Addressed Jason's pedantic correction over the wording of a comment.
DanielRosenwasser 40796b2
Added more tests.
DanielRosenwasser 9d8319d
Perform checking, document function.
DanielRosenwasser 0ffa722
Removed superfluous type assertion.
DanielRosenwasser 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
This file contains 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
31 changes: 31 additions & 0 deletions
31
tests/baselines/reference/superCallArgsMustMatch.errors.txt
This file contains 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,31 @@ | ||
tests/cases/compiler/superCallArgsMustMatch.ts(17,15): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. | ||
|
||
|
||
==== tests/cases/compiler/superCallArgsMustMatch.ts (1 errors) ==== | ||
class T5<T>{ | ||
|
||
public foo: T; | ||
|
||
constructor(public bar: T) { } | ||
|
||
} | ||
|
||
|
||
|
||
class T6 extends T5<number>{ | ||
|
||
constructor() { | ||
|
||
// Should error; base constructor has type T for first arg, | ||
// which is instantiated with 'number' in the extends clause | ||
super("hi"); | ||
~~~~ | ||
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. | ||
|
||
var x: number = this.foo; | ||
|
||
} | ||
|
||
} | ||
|
||
|
This file contains 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
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
...rCallFromClassThatDerivesFromGenericTypeButWithIncorrectNumberOfTypeArguments1.errors.txt
This file contains 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,19 @@ | ||
tests/cases/compiler/superCallFromClassThatDerivesFromGenericTypeButWithIncorrectNumberOfTypeArguments1.ts(8,17): error TS2314: Generic type 'A<T1, T2>' requires 2 type argument(s). | ||
tests/cases/compiler/superCallFromClassThatDerivesFromGenericTypeButWithIncorrectNumberOfTypeArguments1.ts(9,21): error TS2335: 'super' can only be referenced in a derived class. | ||
|
||
|
||
==== tests/cases/compiler/superCallFromClassThatDerivesFromGenericTypeButWithIncorrectNumberOfTypeArguments1.ts (2 errors) ==== | ||
|
||
class A<T1, T2> { | ||
constructor(private map: (value: T1) => T2) { | ||
|
||
} | ||
} | ||
|
||
class B extends A<number> { | ||
~~~~~~~~~ | ||
!!! error TS2314: Generic type 'A<T1, T2>' requires 2 type argument(s). | ||
constructor() { super(value => String(value)); } | ||
~~~~~ | ||
!!! error TS2335: 'super' can only be referenced in a derived class. | ||
} |
32 changes: 32 additions & 0 deletions
32
...nce/superCallFromClassThatDerivesFromGenericTypeButWithIncorrectNumberOfTypeArguments1.js
This file contains 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,32 @@ | ||
//// [superCallFromClassThatDerivesFromGenericTypeButWithIncorrectNumberOfTypeArguments1.ts] | ||
|
||
class A<T1, T2> { | ||
constructor(private map: (value: T1) => T2) { | ||
|
||
} | ||
} | ||
|
||
class B extends A<number> { | ||
constructor() { super(value => String(value)); } | ||
} | ||
|
||
//// [superCallFromClassThatDerivesFromGenericTypeButWithIncorrectNumberOfTypeArguments1.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 A = (function () { | ||
function A(map) { | ||
this.map = map; | ||
} | ||
return A; | ||
})(); | ||
var B = (function (_super) { | ||
__extends(B, _super); | ||
function B() { | ||
_super.call(this, function (value) { return String(value); }); | ||
} | ||
return B; | ||
})(A); |
19 changes: 19 additions & 0 deletions
19
.../reference/superCallFromClassThatDerivesFromGenericTypeButWithNoTypeArguments1.errors.txt
This file contains 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,19 @@ | ||
tests/cases/compiler/superCallFromClassThatDerivesFromGenericTypeButWithNoTypeArguments1.ts(8,17): error TS2314: Generic type 'A<T1, T2>' requires 2 type argument(s). | ||
tests/cases/compiler/superCallFromClassThatDerivesFromGenericTypeButWithNoTypeArguments1.ts(9,21): error TS2335: 'super' can only be referenced in a derived class. | ||
|
||
|
||
==== tests/cases/compiler/superCallFromClassThatDerivesFromGenericTypeButWithNoTypeArguments1.ts (2 errors) ==== | ||
|
||
class A<T1, T2> { | ||
constructor(private map: (value: T1) => T2) { | ||
|
||
} | ||
} | ||
|
||
class B extends A { | ||
~ | ||
!!! error TS2314: Generic type 'A<T1, T2>' requires 2 type argument(s). | ||
constructor() { super(value => String(value)); } | ||
~~~~~ | ||
!!! error TS2335: 'super' can only be referenced in a derived class. | ||
} |
32 changes: 32 additions & 0 deletions
32
...aselines/reference/superCallFromClassThatDerivesFromGenericTypeButWithNoTypeArguments1.js
This file contains 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,32 @@ | ||
//// [superCallFromClassThatDerivesFromGenericTypeButWithNoTypeArguments1.ts] | ||
|
||
class A<T1, T2> { | ||
constructor(private map: (value: T1) => T2) { | ||
|
||
} | ||
} | ||
|
||
class B extends A { | ||
constructor() { super(value => String(value)); } | ||
} | ||
|
||
//// [superCallFromClassThatDerivesFromGenericTypeButWithNoTypeArguments1.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 A = (function () { | ||
function A(map) { | ||
this.map = map; | ||
} | ||
return A; | ||
})(); | ||
var B = (function (_super) { | ||
__extends(B, _super); | ||
function B() { | ||
_super.call(this, function (value) { return String(value); }); | ||
} | ||
return B; | ||
})(A); |
19 changes: 19 additions & 0 deletions
19
...nes/reference/superCallFromClassThatDerivesNonGenericTypeButWithTypeArguments1.errors.txt
This file contains 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,19 @@ | ||
tests/cases/compiler/superCallFromClassThatDerivesNonGenericTypeButWithTypeArguments1.ts(8,17): error TS2315: Type 'A' is not generic. | ||
tests/cases/compiler/superCallFromClassThatDerivesNonGenericTypeButWithTypeArguments1.ts(9,21): error TS2335: 'super' can only be referenced in a derived class. | ||
|
||
|
||
==== tests/cases/compiler/superCallFromClassThatDerivesNonGenericTypeButWithTypeArguments1.ts (2 errors) ==== | ||
|
||
class A { | ||
constructor(private map: (value: number) => string) { | ||
|
||
} | ||
} | ||
|
||
class B extends A<number, string> { | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS2315: Type 'A' is not generic. | ||
constructor() { super(value => String(value)); } | ||
~~~~~ | ||
!!! error TS2335: 'super' can only be referenced in a derived class. | ||
} |
32 changes: 32 additions & 0 deletions
32
...s/baselines/reference/superCallFromClassThatDerivesNonGenericTypeButWithTypeArguments1.js
This file contains 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,32 @@ | ||
//// [superCallFromClassThatDerivesNonGenericTypeButWithTypeArguments1.ts] | ||
|
||
class A { | ||
constructor(private map: (value: number) => string) { | ||
|
||
} | ||
} | ||
|
||
class B extends A<number, string> { | ||
constructor() { super(value => String(value)); } | ||
} | ||
|
||
//// [superCallFromClassThatDerivesNonGenericTypeButWithTypeArguments1.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 A = (function () { | ||
function A(map) { | ||
this.map = map; | ||
} | ||
return A; | ||
})(); | ||
var B = (function (_super) { | ||
__extends(B, _super); | ||
function B() { | ||
_super.call(this, function (value) { return String(value); }); | ||
} | ||
return B; | ||
})(A); |
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/superCallFromClassThatHasNoBaseType1.errors.txt
This file contains 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,16 @@ | ||
tests/cases/compiler/superCallFromClassThatHasNoBaseType1.ts(9,21): error TS2335: 'super' can only be referenced in a derived class. | ||
|
||
|
||
==== tests/cases/compiler/superCallFromClassThatHasNoBaseType1.ts (1 errors) ==== | ||
|
||
class A { | ||
constructor(private map: (value: number) => string) { | ||
|
||
} | ||
} | ||
|
||
class B { | ||
constructor() { super(value => String(value)); } | ||
~~~~~ | ||
!!! error TS2335: 'super' can only be referenced in a derived class. | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/baselines/reference/superCallFromClassThatHasNoBaseType1.js
This file contains 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,25 @@ | ||
//// [superCallFromClassThatHasNoBaseType1.ts] | ||
|
||
class A { | ||
constructor(private map: (value: number) => string) { | ||
|
||
} | ||
} | ||
|
||
class B { | ||
constructor() { super(value => String(value)); } | ||
} | ||
|
||
//// [superCallFromClassThatHasNoBaseType1.js] | ||
var A = (function () { | ||
function A(map) { | ||
this.map = map; | ||
} | ||
return A; | ||
})(); | ||
var B = (function () { | ||
function B() { | ||
_super.call(this, function (value) { return String(value); }); | ||
} | ||
return B; | ||
})(); |
10 changes: 10 additions & 0 deletions
10
tests/baselines/reference/superCallFromFunction1.errors.txt
This file contains 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,10 @@ | ||
tests/cases/compiler/superCallFromFunction1.ts(3,5): error TS2335: 'super' can only be referenced in a derived class. | ||
|
||
|
||
==== tests/cases/compiler/superCallFromFunction1.ts (1 errors) ==== | ||
|
||
function foo() { | ||
super(value => String(value)); | ||
~~~~~ | ||
!!! error TS2335: 'super' can only be referenced in a derived class. | ||
} |
This file contains 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,10 @@ | ||
//// [superCallFromFunction1.ts] | ||
|
||
function foo() { | ||
super(value => String(value)); | ||
} | ||
|
||
//// [superCallFromFunction1.js] | ||
function foo() { | ||
_super.call(this, function (value) { return String(value); }); | ||
} |
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.
This is fine, but one alternative you might consider is for resolveCall to take a parameter for the type arguments, and have the caller determine what to pass. So whichever caller handles super calls would get the type arguments from the base type reference. But I admit it's a bit weird to pass the entire CallLikeExpression, with the the type arguments alongside it. It's like having meat and potatoes, with a side of potatoes.
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.
Exactly! It's like if you gave me corn alongside corn-off-the-cob - it's kind of odd to give both.
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.
We can do this without passing the type arguments. But I think we'll leave it for another PR.