-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Forbid this as constructor parameter type #5474
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
c82d4a6
Forbid 'this' as constructor parameter type
sandersn e609047
Add tests based on #5449
sandersn 84b8947
Accept baselines
sandersn 6aecd43
Fix `isConstructorParameter`
sandersn 201266b
Switch to `isNodeDescendantOf`
sandersn 67b9647
Add a variable of type `this` in constructor body
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
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
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
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
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,23 @@ | ||
tests/cases/conformance/types/thisType/thisTypeErrors2.ts(2,20): error TS2526: A 'this' type is available only in a non-static member of a class or interface. | ||
tests/cases/conformance/types/thisType/thisTypeErrors2.ts(9,38): error TS2526: A 'this' type is available only in a non-static member of a class or interface. | ||
|
||
|
||
==== tests/cases/conformance/types/thisType/thisTypeErrors2.ts (2 errors) ==== | ||
class Base { | ||
constructor(a: this) { | ||
~~~~ | ||
!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface. | ||
} | ||
} | ||
class Generic<T> { | ||
} | ||
class Derived { | ||
n: number; | ||
constructor(public host: Generic<this>) { | ||
~~~~ | ||
!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface. | ||
let self: this = this; | ||
this.n = 12; | ||
} | ||
} | ||
|
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,35 @@ | ||
//// [thisTypeErrors2.ts] | ||
class Base { | ||
constructor(a: this) { | ||
} | ||
} | ||
class Generic<T> { | ||
} | ||
class Derived { | ||
n: number; | ||
constructor(public host: Generic<this>) { | ||
let self: this = this; | ||
this.n = 12; | ||
} | ||
} | ||
|
||
|
||
//// [thisTypeErrors2.js] | ||
var Base = (function () { | ||
function Base(a) { | ||
} | ||
return Base; | ||
})(); | ||
var Generic = (function () { | ||
function Generic() { | ||
} | ||
return Generic; | ||
})(); | ||
var Derived = (function () { | ||
function Derived(host) { | ||
this.host = host; | ||
var self = this; | ||
this.n = 12; | ||
} | ||
return Derived; | ||
})(); |
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,57 @@ | ||
tests/cases/conformance/types/thisType/thisTypeInClasses.ts(4,20): error TS2526: A 'this' type is available only in a non-static member of a class or interface. | ||
|
||
|
||
==== tests/cases/conformance/types/thisType/thisTypeInClasses.ts (1 errors) ==== | ||
class C1 { | ||
x: this; | ||
f(x: this): this { return undefined; } | ||
constructor(x: this) { } | ||
~~~~ | ||
!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface. | ||
} | ||
|
||
class C2 { | ||
[x: string]: this; | ||
} | ||
|
||
interface Foo<T> { | ||
x: T; | ||
y: this; | ||
} | ||
|
||
class C3 { | ||
a: this[]; | ||
b: [this, this]; | ||
c: this | Date; | ||
d: this & Date; | ||
e: (((this))); | ||
f: (x: this) => this; | ||
g: new (x: this) => this; | ||
h: Foo<this>; | ||
i: Foo<this | (() => this)>; | ||
j: (x: any) => x is this; | ||
} | ||
|
||
declare class C4 { | ||
x: this; | ||
f(x: this): this; | ||
} | ||
|
||
class C5 { | ||
foo() { | ||
let f1 = (x: this): this => this; | ||
let f2 = (x: this) => this; | ||
let f3 = (x: this) => (y: this) => this; | ||
let f4 = (x: this) => { | ||
let g = (y: this) => { | ||
return () => this; | ||
} | ||
return g(this); | ||
} | ||
} | ||
bar() { | ||
let x1 = <this>undefined; | ||
let x2 = undefined as this; | ||
} | ||
} | ||
|
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,13 @@ | ||
class Base { | ||
constructor(a: this) { | ||
} | ||
} | ||
class Generic<T> { | ||
} | ||
class Derived { | ||
n: number; | ||
constructor(public host: Generic<this>) { | ||
let self: this = this; | ||
this.n = 12; | ||
} | ||
} |
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.
Maybe use
this
in a type annotation of a local variable to test thatthis
can be referenced in the body of a constructor.