Microsoft/TypeScript
Vedi su GitHubMixed es5/es2015 code with mixin classes causes runtime errors
Open
#17.088 aperta il 11 lug 2017
Awaiting More FeedbackEffort: ModerateHelp WantedSuggestion
Metriche repository
- Star
- (48.455 star)
- Metriche merge PR
- (Merge medio 6g 17h) (9 PR mergiate in 30 g)
Descrizione
TypeScript 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'.