question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ESNext emits optional class members

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
DanielRosenwassercommented, Jan 8, 2022

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.

class C { 
  declare x?: number; 
}
2reactions
DanielRosenwassercommented, Jan 8, 2022

Sounds like this is possibly a bug in --exactOptionalPropertyTypes.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found