-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Contextual typing of partially annotated signatures #11673
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8094b2c
Improve contextual typing of partially annotated signatures
ahejlsberg 808db07
Accept new baselines
ahejlsberg 6425f0c
Fix lint error
ahejlsberg df58df1
Add partially annotated function tests
HerringtonDarkholme 8aa56c1
Add fourslash test for contextually-typed `this`
sandersn af52b63
Address CR feedback
ahejlsberg 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 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
27 changes: 27 additions & 0 deletions
27
tests/baselines/reference/partiallyAnnotatedFunctionInferenceError.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,27 @@ | ||
tests/cases/conformance/types/contextualTypes/partiallyAnnotatedFunction/partiallyAnnotatedFunctionInferenceError.ts(12,11): error TS2345: Argument of type '(t1: D, t2: D, t3: any) => void' is not assignable to parameter of type '(t: D, t1: D) => void'. | ||
tests/cases/conformance/types/contextualTypes/partiallyAnnotatedFunction/partiallyAnnotatedFunctionInferenceError.ts(13,11): error TS2345: Argument of type '(t1: D, t2: D, t3: any) => void' is not assignable to parameter of type '(t: D, t1: D) => void'. | ||
tests/cases/conformance/types/contextualTypes/partiallyAnnotatedFunction/partiallyAnnotatedFunctionInferenceError.ts(14,11): error TS2345: Argument of type '(t1: C, t2: C, t3: D) => void' is not assignable to parameter of type '(t: C, t1: C) => void'. | ||
|
||
|
||
==== tests/cases/conformance/types/contextualTypes/partiallyAnnotatedFunction/partiallyAnnotatedFunctionInferenceError.ts (3 errors) ==== | ||
class C { | ||
test: string | ||
} | ||
|
||
class D extends C { | ||
test2: string | ||
} | ||
|
||
declare function testError<T extends C>(a: (t: T, t1: T) => void): T | ||
|
||
// more args | ||
testError((t1: D, t2, t3) => {}) | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2345: Argument of type '(t1: D, t2: D, t3: any) => void' is not assignable to parameter of type '(t: D, t1: D) => void'. | ||
testError((t1, t2: D, t3) => {}) | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2345: Argument of type '(t1: D, t2: D, t3: any) => void' is not assignable to parameter of type '(t: D, t1: D) => void'. | ||
testError((t1, t2, t3: D) => {}) | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2345: Argument of type '(t1: C, t2: C, t3: D) => void' is not assignable to parameter of type '(t: C, t1: C) => void'. | ||
|
39 changes: 39 additions & 0 deletions
39
tests/baselines/reference/partiallyAnnotatedFunctionInferenceError.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,39 @@ | ||
//// [partiallyAnnotatedFunctionInferenceError.ts] | ||
class C { | ||
test: string | ||
} | ||
|
||
class D extends C { | ||
test2: string | ||
} | ||
|
||
declare function testError<T extends C>(a: (t: T, t1: T) => void): T | ||
|
||
// more args | ||
testError((t1: D, t2, t3) => {}) | ||
testError((t1, t2: D, t3) => {}) | ||
testError((t1, t2, t3: D) => {}) | ||
|
||
|
||
//// [partiallyAnnotatedFunctionInferenceError.js] | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var C = (function () { | ||
function C() { | ||
} | ||
return C; | ||
}()); | ||
var D = (function (_super) { | ||
__extends(D, _super); | ||
function D() { | ||
return _super.apply(this, arguments) || this; | ||
} | ||
return D; | ||
}(C)); | ||
// more args | ||
testError(function (t1, t2, t3) { }); | ||
testError(function (t1, t2, t3) { }); | ||
testError(function (t1, t2, t3) { }); |
85 changes: 85 additions & 0 deletions
85
tests/baselines/reference/partiallyAnnotatedFunctionInferenceWithTypeParameter.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,85 @@ | ||
//// [partiallyAnnotatedFunctionInferenceWithTypeParameter.ts] | ||
class C { | ||
test: string | ||
} | ||
|
||
class D extends C { | ||
test2: string | ||
} | ||
|
||
declare function test<T extends C>(a: (t: T, t1: T) => void): T | ||
|
||
declare function testRest<T extends C>(a: (t: T, t1: T, ...ts: T[]) => void): T | ||
|
||
|
||
// exactly | ||
test((t1: D, t2) => { t2.test2 }) | ||
test((t1, t2: D) => { t2.test2 }) | ||
|
||
// zero arg | ||
test(() => {}) | ||
|
||
// fewer args | ||
test((t1: D) => {}) | ||
|
||
// rest arg | ||
test((...ts: D[]) => {}) | ||
|
||
// source function has rest arg | ||
testRest((t1: D) => {}) | ||
testRest((t1, t2, t3) => {}) | ||
testRest((t1: D, t2, t3) => {}) | ||
testRest((t1, t2: D, t3) => {}) | ||
testRest((t2: D, ...t3) => {}) | ||
testRest((t2, ...t3: D[]) => {}) | ||
|
||
|
||
//// [partiallyAnnotatedFunctionInferenceWithTypeParameter.js] | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var C = (function () { | ||
function C() { | ||
} | ||
return C; | ||
}()); | ||
var D = (function (_super) { | ||
__extends(D, _super); | ||
function D() { | ||
return _super.apply(this, arguments) || this; | ||
} | ||
return D; | ||
}(C)); | ||
// exactly | ||
test(function (t1, t2) { t2.test2; }); | ||
test(function (t1, t2) { t2.test2; }); | ||
// zero arg | ||
test(function () { }); | ||
// fewer args | ||
test(function (t1) { }); | ||
// rest arg | ||
test(function () { | ||
var ts = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
ts[_i - 0] = arguments[_i]; | ||
} | ||
}); | ||
// source function has rest arg | ||
testRest(function (t1) { }); | ||
testRest(function (t1, t2, t3) { }); | ||
testRest(function (t1, t2, t3) { }); | ||
testRest(function (t1, t2, t3) { }); | ||
testRest(function (t2) { | ||
var t3 = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
t3[_i - 1] = arguments[_i]; | ||
} | ||
}); | ||
testRest(function (t2) { | ||
var t3 = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
t3[_i - 1] = arguments[_i]; | ||
} | ||
}); |
114 changes: 114 additions & 0 deletions
114
tests/baselines/reference/partiallyAnnotatedFunctionInferenceWithTypeParameter.symbols
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,114 @@ | ||
=== tests/cases/conformance/types/contextualTypes/partiallyAnnotatedFunction/partiallyAnnotatedFunctionInferenceWithTypeParameter.ts === | ||
class C { | ||
>C : Symbol(C, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 0, 0)) | ||
|
||
test: string | ||
>test : Symbol(C.test, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 0, 9)) | ||
} | ||
|
||
class D extends C { | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
>C : Symbol(C, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 0, 0)) | ||
|
||
test2: string | ||
>test2 : Symbol(D.test2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 4, 19)) | ||
} | ||
|
||
declare function test<T extends C>(a: (t: T, t1: T) => void): T | ||
>test : Symbol(test, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 6, 1)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 22)) | ||
>C : Symbol(C, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 0, 0)) | ||
>a : Symbol(a, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 35)) | ||
>t : Symbol(t, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 39)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 22)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 44)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 22)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 22)) | ||
|
||
declare function testRest<T extends C>(a: (t: T, t1: T, ...ts: T[]) => void): T | ||
>testRest : Symbol(testRest, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 63)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 26)) | ||
>C : Symbol(C, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 0, 0)) | ||
>a : Symbol(a, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 39)) | ||
>t : Symbol(t, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 43)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 26)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 48)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 26)) | ||
>ts : Symbol(ts, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 55)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 26)) | ||
>T : Symbol(T, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 10, 26)) | ||
|
||
|
||
// exactly | ||
test((t1: D, t2) => { t2.test2 }) | ||
>test : Symbol(test, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 6, 1)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 14, 6)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 14, 12)) | ||
>t2.test2 : Symbol(D.test2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 4, 19)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 14, 12)) | ||
>test2 : Symbol(D.test2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 4, 19)) | ||
|
||
test((t1, t2: D) => { t2.test2 }) | ||
>test : Symbol(test, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 6, 1)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 15, 6)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 15, 9)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
>t2.test2 : Symbol(D.test2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 4, 19)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 15, 9)) | ||
>test2 : Symbol(D.test2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 4, 19)) | ||
|
||
// zero arg | ||
test(() => {}) | ||
>test : Symbol(test, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 6, 1)) | ||
|
||
// fewer args | ||
test((t1: D) => {}) | ||
>test : Symbol(test, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 6, 1)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 21, 6)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
|
||
// rest arg | ||
test((...ts: D[]) => {}) | ||
>test : Symbol(test, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 6, 1)) | ||
>ts : Symbol(ts, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 24, 6)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
|
||
// source function has rest arg | ||
testRest((t1: D) => {}) | ||
>testRest : Symbol(testRest, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 63)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 27, 10)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
|
||
testRest((t1, t2, t3) => {}) | ||
>testRest : Symbol(testRest, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 63)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 28, 10)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 28, 13)) | ||
>t3 : Symbol(t3, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 28, 17)) | ||
|
||
testRest((t1: D, t2, t3) => {}) | ||
>testRest : Symbol(testRest, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 63)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 29, 10)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 29, 16)) | ||
>t3 : Symbol(t3, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 29, 20)) | ||
|
||
testRest((t1, t2: D, t3) => {}) | ||
>testRest : Symbol(testRest, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 63)) | ||
>t1 : Symbol(t1, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 30, 10)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 30, 13)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
>t3 : Symbol(t3, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 30, 20)) | ||
|
||
testRest((t2: D, ...t3) => {}) | ||
>testRest : Symbol(testRest, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 63)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 31, 10)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
>t3 : Symbol(t3, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 31, 16)) | ||
|
||
testRest((t2, ...t3: D[]) => {}) | ||
>testRest : Symbol(testRest, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 8, 63)) | ||
>t2 : Symbol(t2, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 32, 10)) | ||
>t3 : Symbol(t3, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 32, 13)) | ||
>D : Symbol(D, Decl(partiallyAnnotatedFunctionInferenceWithTypeParameter.ts, 2, 1)) | ||
|
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 can be written as
!parameterIsThisKeyword(firstOrUndefined(node.parameters))