-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
allow this
in typeQuery
#43898
Merged
Merged
allow this
in typeQuery
#43898
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f0cc28a
allow `this` in typeQuery
Zzzen e863a9c
add tests
Zzzen 83120f4
get this type as expression
Zzzen 30a7522
Merge remote-tracking branch 'origin/master' into allow-typeof-this
Zzzen ed29f38
handle nested nodes
Zzzen 4d3c53c
Merge remote-tracking branch 'origin/main' into allow-typeof-this
Zzzen 1c63a5b
update baselines
Zzzen 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
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 |
---|---|---|
|
@@ -4348,6 +4348,12 @@ namespace ts { | |
return !!node && node.kind === SyntaxKind.Identifier && identifierIsThisKeyword(node as Identifier); | ||
} | ||
|
||
export function isThisInTypeQuery(node: Node): boolean { | ||
return isThisIdentifier(node) && | ||
(node.parent.kind === SyntaxKind.TypeQuery || | ||
(node.parent.kind === SyntaxKind.QualifiedName && (node.parent as QualifiedName).left === node)); | ||
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. the QualifiedName case doesn't check that its ultimate parent is a TypeQuery, so I think you could maybe fool this with |
||
} | ||
|
||
export function identifierIsThisKeyword(id: Identifier): boolean { | ||
return id.originalKeywordKind === SyntaxKind.ThisKeyword; | ||
} | ||
|
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 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 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 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 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 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,113 @@ | ||
tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts(23,19): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. | ||
tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts(45,23): error TS2331: 'this' cannot be referenced in a module or namespace body. | ||
tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts(45,23): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. | ||
tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts(51,23): error TS2331: 'this' cannot be referenced in a module or namespace body. | ||
tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts(51,23): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. | ||
tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts(56,19): error TS7041: The containing arrow function captures the global value of 'this'. | ||
|
||
|
||
==== tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts (6 errors) ==== | ||
class Test { | ||
data = {}; | ||
constructor() { | ||
var copy: typeof this.data = {}; | ||
} | ||
} | ||
|
||
class Test1 { | ||
data = { foo: '' }; | ||
['this'] = ''; | ||
constructor() { | ||
var copy: typeof this.data = { foo: '' }; | ||
|
||
var self: typeof this = this; | ||
self.data; | ||
|
||
var str: typeof this.this = ''; | ||
} | ||
} | ||
|
||
|
||
function Test2() { | ||
let x: typeof this.no = 1; | ||
~~~~ | ||
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. | ||
} | ||
|
||
function Test3(this: { no: number }) { | ||
let x: typeof this.no = 1; | ||
} | ||
|
||
function Test4(this: { no: number } | undefined) { | ||
let x: typeof this.no = 1; | ||
} | ||
|
||
class Test5 { | ||
no = 1; | ||
|
||
f = () => { | ||
// should not capture this. | ||
let x: typeof this.no = 1; | ||
} | ||
} | ||
|
||
namespace Test6 { | ||
export let f = () => { | ||
let x: typeof this.no = 1; | ||
~~~~ | ||
!!! error TS2331: 'this' cannot be referenced in a module or namespace body. | ||
~~~~ | ||
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. | ||
} | ||
} | ||
|
||
module Test7 { | ||
export let f = () => { | ||
let x: typeof this.no = 1; | ||
~~~~ | ||
!!! error TS2331: 'this' cannot be referenced in a module or namespace body. | ||
~~~~ | ||
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. | ||
} | ||
} | ||
|
||
const Test8 = () => { | ||
let x: typeof this.no = 1; | ||
~~~~ | ||
!!! error TS7041: The containing arrow function captures the global value of 'this'. | ||
} | ||
|
||
class Test9 { | ||
no = 0; | ||
this = 0; | ||
|
||
f() { | ||
if (this instanceof Test9D1) { | ||
const d1: typeof this = this; | ||
d1.f1(); | ||
} | ||
|
||
if (this instanceof Test9D2) { | ||
const d2: typeof this = this; | ||
d2.f2(); | ||
} | ||
} | ||
|
||
g() { | ||
if (this.no === 1) { | ||
const no: typeof this.no = this.no; | ||
} | ||
|
||
if (this.this === 1) { | ||
const no: typeof this.this = this.this; | ||
} | ||
} | ||
} | ||
|
||
class Test9D1 { | ||
f1() {} | ||
} | ||
|
||
class Test9D2 { | ||
f2() {} | ||
} |
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.
does this work when the QualifiedName is nested, like
this.x.y
? I think you need a test for 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.
It doesn't work for
this.x.y
. I've replacedisTypeQueryNode
withisPartOfTypeQuery