microsoft / microsoft/TypeScript
Class field initialization order different between target ES2021 and ES2022
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
ES2022 class field initialization order
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
⏯ Playground Link
Playground link with relevant code
💻 Code
interface Param {
a: number;
}
class Test {
// constructor that sets a field
constructor(private param: Param){
}
// use that field to set another field
a = this.param.a;
}
const t = new Test({a: 10});
console.log(t.a);
🙁 Actual behavior
If the compiler is set to target "ES2021" then the output will be:
"use strict";
class Test {
constructor(param) {
this.param = param;
this.a = this.param.a;
}
}
const t = new Test({ a: 10 });
console.log(t.a);
If the target is set to "ES2022" then the output will be:
"use strict";
class Test {
param;
constructor(param) {
this.param = param;
}
a = this.param.a;
}
const t = new Test({ a: 10 });
console.log(t.a);
These look logically the same, just utilizing the field syntax in Javascript, but in Javascript fields are initialized BEFORE the constructor is run.
So that means that ES2021 will work, setting the param field on Test and then use it to set the a field, but with ES2022 it will attempt to set a first, using the param field which has not been initialized yet, thus throwing an error at runtime.
There is no warning or error that this will happen, and I could not find any kind of compiler flag to enable such an error. Even "strictPropertyInitialization" didn't catch this like I thought it might.
🙂 Expected behavior
In order to maintain Javascript semantics, I believe that Typescript should not allow field initializers to access these automatic field setting constructor parameters. Typescript should not allow you to do something that will simply not work.
Alternatively, the compiler could be changed to insert the initializer at the end of the constructor. This would maintain existing Typescript behavior at the expense of being different from Javascript.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground and compare the ES2021 and ES2022 outputs for the class using a parameter property in its field initializer. Done means determining the intended behavior or diagnostic for this semantic mismatch and covering the reproduced example with a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100