-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Instantiate this for contextually typed type parameters #9746
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
sandersn
merged 6 commits into
master
from
instantiate-this-for-contextually-typed-type-parameters
Aug 22, 2016
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad56220
Instantiate contextual this parameters if needed
sandersn 78f807c
Test that contextually typed generic this parameters are instantiated
sandersn 271ffc8
Merge branch 'master' into instantiate-this-for-contextually-typed-ty…
sandersn 0f483d6
Assign and instantiate contextual this type if not present
sandersn 5aafc2c
Contextually type this in getDeclFromSig, not checkThisExpr
sandersn fc1d6a8
Rename getContextuallyTypedThisParameter to getContextualThisParameter
sandersn 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3063,9 +3063,14 @@ namespace ts { | |
} | ||
} | ||
// Use contextual parameter type if one is available | ||
const type = declaration.symbol.name === "this" | ||
? getContextuallyTypedThisType(func) | ||
: getContextuallyTypedParameterType(<ParameterDeclaration>declaration); | ||
let type: Type; | ||
if (declaration.symbol.name === "this") { | ||
const thisParameter = getContextualThisParameter(func); | ||
type = thisParameter ? getTypeOfSymbol(thisParameter) : undefined; | ||
} | ||
else { | ||
type = getContextuallyTypedParameterType(<ParameterDeclaration>declaration); | ||
} | ||
if (type) { | ||
return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); | ||
} | ||
|
@@ -4689,6 +4694,9 @@ namespace ts { | |
if (isJSConstructSignature) { | ||
minArgumentCount--; | ||
} | ||
if (!thisParameter && isObjectLiteralMethod(declaration)) { | ||
thisParameter = getContextualThisParameter(declaration); | ||
} | ||
|
||
const classType = declaration.kind === SyntaxKind.Constructor ? | ||
getDeclaredTypeOfClassOrInterface(getMergedSymbol((<ClassDeclaration>declaration.parent).symbol)) | ||
|
@@ -9082,10 +9090,6 @@ namespace ts { | |
return getInferredClassType(classSymbol); | ||
} | ||
} | ||
const type = getContextuallyTypedThisType(container); | ||
if (type) { | ||
return type; | ||
} | ||
|
||
const thisType = getThisTypeOfDeclaration(container); | ||
if (thisType) { | ||
|
@@ -9326,11 +9330,11 @@ namespace ts { | |
} | ||
} | ||
|
||
function getContextuallyTypedThisType(func: FunctionLikeDeclaration): Type { | ||
function getContextualThisParameter(func: FunctionLikeDeclaration): Symbol { | ||
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. I think a better name would be 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. done |
||
if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== SyntaxKind.ArrowFunction) { | ||
const contextualSignature = getContextualSignature(func); | ||
if (contextualSignature) { | ||
return getThisTypeOfSignature(contextualSignature); | ||
return contextualSignature.thisParameter; | ||
} | ||
} | ||
|
||
|
@@ -12273,6 +12277,12 @@ namespace ts { | |
|
||
function assignContextualParameterTypes(signature: Signature, context: Signature, mapper: TypeMapper) { | ||
const len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0); | ||
if (context.thisParameter) { | ||
if (!signature.thisParameter) { | ||
signature.thisParameter = createTransientSymbol(context.thisParameter, undefined); | ||
} | ||
assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter), mapper); | ||
} | ||
for (let i = 0; i < len; i++) { | ||
const parameter = signature.parameters[i]; | ||
const contextualParameterType = getTypeAtPosition(context, i); | ||
|
20 changes: 20 additions & 0 deletions
20
tests/baselines/reference/instantiateContextuallyTypedGenericThis.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,20 @@ | ||
//// [instantiateContextuallyTypedGenericThis.ts] | ||
interface JQuery { | ||
each<T>( | ||
collection: T[], callback: (this: T, dit: T) => T | ||
): T[]; | ||
} | ||
|
||
let $: JQuery; | ||
let lines: string[]; | ||
$.each(lines, function(dit) { | ||
return dit.charAt(0) + this.charAt(1); | ||
}); | ||
|
||
|
||
//// [instantiateContextuallyTypedGenericThis.js] | ||
var $; | ||
var lines; | ||
$.each(lines, function (dit) { | ||
return dit.charAt(0) + this.charAt(1); | ||
}); |
46 changes: 46 additions & 0 deletions
46
tests/baselines/reference/instantiateContextuallyTypedGenericThis.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,46 @@ | ||
=== tests/cases/compiler/instantiateContextuallyTypedGenericThis.ts === | ||
interface JQuery { | ||
>JQuery : Symbol(JQuery, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 0)) | ||
|
||
each<T>( | ||
>each : Symbol(JQuery.each, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 18)) | ||
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9)) | ||
|
||
collection: T[], callback: (this: T, dit: T) => T | ||
>collection : Symbol(collection, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 12)) | ||
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9)) | ||
>callback : Symbol(callback, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 24)) | ||
>this : Symbol(this, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 36)) | ||
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9)) | ||
>dit : Symbol(dit, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 44)) | ||
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9)) | ||
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9)) | ||
|
||
): T[]; | ||
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9)) | ||
} | ||
|
||
let $: JQuery; | ||
>$ : Symbol($, Decl(instantiateContextuallyTypedGenericThis.ts, 6, 3)) | ||
>JQuery : Symbol(JQuery, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 0)) | ||
|
||
let lines: string[]; | ||
>lines : Symbol(lines, Decl(instantiateContextuallyTypedGenericThis.ts, 7, 3)) | ||
|
||
$.each(lines, function(dit) { | ||
>$.each : Symbol(JQuery.each, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 18)) | ||
>$ : Symbol($, Decl(instantiateContextuallyTypedGenericThis.ts, 6, 3)) | ||
>each : Symbol(JQuery.each, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 18)) | ||
>lines : Symbol(lines, Decl(instantiateContextuallyTypedGenericThis.ts, 7, 3)) | ||
>dit : Symbol(dit, Decl(instantiateContextuallyTypedGenericThis.ts, 8, 23)) | ||
|
||
return dit.charAt(0) + this.charAt(1); | ||
>dit.charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --)) | ||
>dit : Symbol(dit, Decl(instantiateContextuallyTypedGenericThis.ts, 8, 23)) | ||
>charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --)) | ||
>this.charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --)) | ||
>this : Symbol(this, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 36)) | ||
>charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --)) | ||
|
||
}); | ||
|
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/instantiateContextuallyTypedGenericThis.types
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,53 @@ | ||
=== tests/cases/compiler/instantiateContextuallyTypedGenericThis.ts === | ||
interface JQuery { | ||
>JQuery : JQuery | ||
|
||
each<T>( | ||
>each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[] | ||
>T : T | ||
|
||
collection: T[], callback: (this: T, dit: T) => T | ||
>collection : T[] | ||
>T : T | ||
>callback : (this: T, dit: T) => T | ||
>this : T | ||
>T : T | ||
>dit : T | ||
>T : T | ||
>T : T | ||
|
||
): T[]; | ||
>T : T | ||
} | ||
|
||
let $: JQuery; | ||
>$ : JQuery | ||
>JQuery : JQuery | ||
|
||
let lines: string[]; | ||
>lines : string[] | ||
|
||
$.each(lines, function(dit) { | ||
>$.each(lines, function(dit) { return dit.charAt(0) + this.charAt(1);}) : string[] | ||
>$.each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[] | ||
>$ : JQuery | ||
>each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[] | ||
>lines : string[] | ||
>function(dit) { return dit.charAt(0) + this.charAt(1);} : (this: string, dit: string) => string | ||
>dit : string | ||
|
||
return dit.charAt(0) + this.charAt(1); | ||
>dit.charAt(0) + this.charAt(1) : string | ||
>dit.charAt(0) : string | ||
>dit.charAt : (pos: number) => string | ||
>dit : string | ||
>charAt : (pos: number) => string | ||
>0 : number | ||
>this.charAt(1) : string | ||
>this.charAt : (pos: number) => string | ||
>this : string | ||
>charAt : (pos: number) => string | ||
>1 : 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
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.
I had to save this block for contextually-typed object literals like this:
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.
Based on our in-person discussion, I checked on contextual typing for parameters. It's 1 of 9 cases in
getTypeForVariableLikeDeclaration
:The contextual typing for an undeclared
this
is basically the same thing here incheckThisExpression
since there is no parameter to get the type of.