ESNext emits optional class members
See original GitHub issueBug Report
🔎 Search Terms
optional class members
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about optional class members
⏯ Playground Link
Playground link with relevant code
💻 Code
// Consider this class
class C {
x?: number;
}
🙁 Actual behavior
// This is the emitted JS if you target ESNext and have --useDefineForClassFields
class C {
x;
}
x
is emitted as a class member, even though it was optional.
This feels wrong, but to make a stronger point consider --exactOptionalPropertyTypes
.
With this compiler option, we should have the typing invariant !("x" in c) || typeof c.x == "number"
; but with this emit, a new instance has the opposite "x" in c && typeof c.x == "undefined"
.
🙂 Expected behavior
Optional members should probably not be emitted:
class C { }
I am not sure / have not thought about what the implications with respect to useDefineForClassFields
semantics are.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Private properties in JavaScript ES6 classes - Stack Overflow
The approach here is to use the scope of the constructor function, which is private, to store private data. For methods to have...
Read more >TSConfig Reference - Docs on every TSConfig option
Properties with relative paths found in the configuration file, ... have an 'override' modifier because it overrides a member in the base class...
Read more >Compiler Options - Microsoft Open Source
The value ESNext refers to whatever the highest version TypeScript supports at the time is. ... Controls how JSX constructs are emitted in...
Read more >Overview - TypeScript
For this reason, TypeScript uses a simpler emit by default that only ... The target option (allowing users to switch out of es5...
Read more >The --strict Compiler Option in TypeScript - Marius Schulz
TypeScript 2.3 introduced a new --strict compiler option that enables a ... Ensure overriding members in derived classes are marked with an ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I believe that this is intended. If you want to signal that this was truly not present (i.e. “this thing will potentially not be set”), then you’ll need to use the
declare
modifier.Sounds like this is possibly a bug in
--exactOptionalPropertyTypes
.