Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime error when mixing into Object with es5 target #18564

Closed
JustASquid opened this issue Sep 19, 2017 · 4 comments
Closed

Runtime error when mixing into Object with es5 target #18564

JustASquid opened this issue Sep 19, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@JustASquid
Copy link

TypeScript Version: 2.5.2

Code

type Constructor<T = {}> = new (...args: any[]) => T;

function Mixin<T extends Constructor>(Base: T) {
	return class extends Base {
		NonArrowFunction() {
			console.log("Non Arrow Function");
		}
		
		ArrowFunction = () => {
			console.log("Arrow Function");
		}
	}
}

class A extends Mixin(Object) {
	constructor() {
		super()
		// No runtime errors
		this.ArrowFunction();
		
		// Runtime error in es5 export
		this.NonArrowFunction();
	}
}

new A();

Expected behavior:
No runtime error when compiled with --target es5 and run with latest node version.

Actual behavior:

TypeError: _this.NonArrowFunction is not a function
    at new A (C:\Users\Daniel\Desktop\mixines5.js:31:15)
    at Object.<anonymous> (C:\Users\Daniel\Desktop\mixines5.js:36:1)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

The runtime error does not happen when mixing into an empty class as opposed to Object.

@DanielRosenwasser
Copy link
Member

Unfortunately this is a duplicate of #10762 and ES5-compliant emit can't properly model subclassing certain built-ins.

Check out the FAQ here: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work

If it's available to you, you can call Object.setPrototypeOf at the end of your constructor.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Sep 19, 2017
@JustASquid
Copy link
Author

@DanielRosenwasser Thank you! What would you say would be the best way to describe mixing into an 'empty class' like this? Using

class Empty {}
class A extends Mixin(A) {}

is a bit verbose.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Sep 19, 2017

@JustASquid what about

class A extends Mixin(class {}) {}

or you can adjust your mixin function, making the argument optional.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 3, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 3, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants