mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-26 20:14:05 -05:00
ESNext+[[Define]]: reference to param props illegal (#36425)
* ESNext+[[Define]]: reference to param props illegal
When target: "esnext" and useDefineForClassFields: true, property
declaration initialisers should not be able to reference parameter
properties; class fields are initialised before the constructor runs,
but parameter properties are not:
```ts
class C {
foo = this.bar
constructor(public bar: string) { }
}
```
emits code that looks like this:
```js
class C {
bar
foo = this.bar
constructor(bar) {
this.bar = bar
}
}
new C('x').foo.length // crashes; foo is undefined
```
This PR adds an error on foo's declaration with ESNext+[[Define]].
* improve test
This commit is contained in:
committed by
GitHub
parent
43fc19c958
commit
368db997ed
@@ -0,0 +1,17 @@
|
||||
// @useDefineForClassFields: true
|
||||
// @target: esnext
|
||||
class C {
|
||||
qux = this.bar // should error
|
||||
bar = this.foo // should error
|
||||
quiz = this.bar // ok
|
||||
m1() {
|
||||
this.foo // ok
|
||||
}
|
||||
constructor(private foo: string) {}
|
||||
quim = this.baz // should error
|
||||
baz = this.foo; // should error
|
||||
quid = this.baz // ok
|
||||
m2() {
|
||||
this.foo // ok
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user