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