Skip to content

Abstract classes compiler: Stop native implementation of an Abstract Class #11733

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

Closed
Jordan-Hall opened this issue Oct 19, 2016 · 1 comment
Closed
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@Jordan-Hall
Copy link

Jordan-Hall commented Oct 19, 2016

I'm willing to write the change to the compiler if people believe this is a good idea. Currently abstract classes are compiled into public classes in JavaScript. I know native Javascript doesn't support Abstract classes but you can make abstract classes not instantiate abstract classes.

Typescript Example:

Abstract class AbstractService {
constructor(http:any){
        this.http = http;
    }
}

Expected behavior:

"use strict";
class AbstractService {
    constructor(http) {
        if (this.constructor.name === "AbstractService") {
                throw new TypeError("Cannot construct an instances of a Abstract class");
         }
        this.http = http;
    }
}
exports.AbstractService = AbstractService;
//# sourceMappingURL=AbstractService .abstract.js.map

Actual behavior:

"use strict";
class AbstractService {
    constructor(http) {
        this.http = http;
    }
}
exports.AbstractService= AbstractService;
//# sourceMappingURL=AbstractService.abstract.js.map

I'm currently use this to make abstract classes in native Javascript. It means we can now make open source projects in TypeScript and not have to worry about people extending from a Abstract class.

You can also do the same with functions. this.functionName === Undefined. You can even make sure the parameters are correct

@Jordan-Hall Jordan-Hall changed the title Abstract classes compiler Abstract classes compiler: Stop native implementation of an Abstract Class Oct 19, 2016
@ghost
Copy link

ghost commented Oct 19, 2016

"not have to worry about people extending from a Abstract class" -- I presume you mean "directly instantiating an abstract class" instead?
I don't see what that would accomplish, since JS users can still do this:

new (class extends AbstractService {})()

which doesn't technically directly instantiate it. You still can't force untyped users to implement all the methods you want an inheriting class to implement.
By the way, if (new.target === AbstractService) works in the latest version of node, but is not yet implemented in TypeScript (#2551).

Generally we don't change the emit to try to make things safe at runtime. Private properties can be accessed, readonly properties can be mutated, and abstract classes can be instantiated.

@mhegazy mhegazy added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints labels Oct 19, 2016
@mhegazy mhegazy closed this as completed Apr 24, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants