TypeScript/tests/cases/conformance/classes/propertyMemberDeclarations/assignParameterPropertyToPropertyDeclarationESNext.ts
Nathan Shively-Sanders 368db997ed
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
2020-01-24 14:53:28 -08:00

18 lines
379 B
TypeScript

// @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
}
}