In target: ES5, Base class constructor's primitive return value should be ignored
See original GitHub issueBug Report
🔎 Search Terms
class, extends, constructor, return primitive
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about class constructors
⏯ Playground Link
Playground link with relevant code
💻 Code
new class extends class {
constructor() {
return 1;
}
} {
constructor() {
super();
console.log(typeof this);
}
}
🙁 Actual behavior
Logs “number”
🙂 Expected behavior
Logs “object”
Issue Analytics
- State:
- Created 10 months ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Chapter 17. Objects and Inheritance - Exploring JS
The value of a property can be any JavaScript value, including a function. ... 3: Constructors—Factories for Instances), are roughly similar to classes...
Read more >Constructor, operator "new" - The Modern JavaScript Tutorial
That can be done using constructor functions and the "new" operator. ... If return is called with a primitive, it's ignored.
Read more >Function.prototype.bind() - JavaScript - MDN Web Docs - Mozilla
The newly bound thisArg value is ignored, because the target function of boundFn2 , which is boundFn , already has a bound this...
Read more >typescript-cheatsheet - GitHub Pages
The cheatsheet contains references to types, classes, decorators, ... You may commonly see this as the return type of functions that do not...
Read more >Classes in ECMAScript 6 (final semantics) - 2ality
You use this class just like an ES5 constructor function: ... class MyClass { get prop() { return 'getter'; } set prop(value) ...
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
You probably should have specified that this only happens with ES5-downleveled code. From the OP it seemed like you meant this happened regardless of
target
(which would indicate it was just normal JS behavior).I can guess the historical context here (tsc implementing classes before the latter getting standardized), but IMO the current behavior doesn’t benefit anyone and is unlikely to be relied on. If
this
is a primitive, it means you can’t mutate it whatsoever. Unless you do something really weird like:But that doesn’t even get correct types! Why would someone use tsc, choose its bizarre subclassing behavior, but also disregard the types?
Built-in subclassing not working is a missed but understandable tradeoff, but this one seems niche and not as breaking-change-prone, while increasing tsc’s conformance.