Skip to content

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

Closed
@zheeeng

Description

@zheeeng

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions