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

[SUG] Add mandatory implementation to abstract class and abstract method. #21663

Closed
zheeeng opened this issue Feb 6, 2018 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@zheeeng
Copy link

zheeeng commented Feb 6, 2018

Search Terms:

abstract + throw + mandatory + force implementation

Suggestion:

In Typescript development environment, we get safe using abstraction stuff, but once we emit our abstract class as an upstream lib to vanilla javascript users, we can't make sure they will make implementation for all abstract methods.

This suggestion is adding mandatory implementation by generating throwing error statements in the abstract class constructor and the abstract methods' function body. At least, it will help in runtime and avoids the problem that caused by inexists/non-implementation of abstract methods.

Expected behavior:

abstract class and abstract method:

abstract class Mandatory {
    constructor() {
    }
    abstract sayHello (): string
}

generate:

var Mandatory = /** @class */ (function () {
    function Mandatory() {
        if (this.constructor === Mandatory) {
            throw Error('Must be implemented!');
        }
    }
    Mandatory.prototype.sayHello = function () {
        throw Error('Must be implemented!');
    };
    return Mandatory;
}());

it forbids vanilla javascript calls like:

// error
const t = new Mandatory()

// error
t.sayHelllo() 

class Test extends Mandatory {
}
// ok
const tt = new Test()
// error
tt.sayHello()
@ghost
Copy link

ghost commented Feb 6, 2018

How is that exception better than TypeError: obj.sayHello is not a function?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 6, 2018
@RyanCavanaugh
Copy link
Member

We thought about this when abstract was added and intentionally didn't do it. See #6 and its linked issues.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 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

2 participants