Mixed es5/es2015 code with mixin classes causes runtime errors
See original GitHub issueTypeScript Version: 2.4.1 (but likely anything since 2.2)
When utilising code targeted at ES5 mixed with code targeted at ES6 causes a runtime error when using mixin classes. The real world use case was using a library that was targeted at ES5 for distribution compatibility reasons while downstream code is being targeted at ES2017 because of running in a known limited environment.
Code
Tagged.ts
// @target: es5
export interface Constructor<T> {
new(...args: any[]): T;
prototype: T;
}
export default function Tagged<T extends Constructor<{}>>(Base: T) {
return class extends Base {
_tag = '';
}
}
TaggedExample.ts
// @target: es2015
import Tagged from './Tagged';
class Example {
log() {
console.log('Hello World!');
}
}
const TaggedExample = Tagged(Example);
const taggedExample = new TaggedExample(); // Uncaught TypeError: Class constructor Example cannot be invoked without 'new'
Expected behavior:
No run-time errors.
Actual behavior:
A runtime error of Uncaught TypeError: Class constructor Example cannot be invoked without 'new'
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:12 (8 by maintainers)
Top Results From Across the Web
Multiple inheritance and mixin classes in Python
When an object needs a specific feature it just delegates it to another class (either explicitly or implicitly), so the code is written...
Read more >The Definitive TypeScript 4.8 Guide - SitePen
create , which would cause a runtime error. When the argument is typed as an object , TypeScript will only allow non-primitive values...
Read more >Mixins - The Modern JavaScript Tutorial
The simplest way to implement a mixin in JavaScript is to make an object with useful methods, so that we can easily merge...
Read more >Groovy mixin combined with Gradle DefaultTask results in ...
If we now install this Gradle task and execute it, we get the following runtime error: Execution failed for task ':myproject:mytask'. > MyTask....
Read more >Documentation - Mixins - TypeScript
How Does A Mixin Work? The pattern relies on using generics with class inheritance to extend a base class. TypeScript's best mixin support...
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 FreeTop 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
Top GitHub Comments
This is very problematic because it means my library (which rely on this) published in es5 cannot be consumed by JavaScript directly at all.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.