Jorge Fuentes González Jorge Fuentes González - 1 month ago 13
TypeScript Question

Is possible to avoid or warn overriding a method or property of an inherited class on TypeScript?

Is there any way to avoid this:

class a {
p:any;
constructor() {
}
m() {
}
}
class b extends a {
p:any;
constructor() {
super();
}
m() {
}
}


I know that I can create private properties, but to access them externally I need to create a public method, so the problem is still there, as methods are "overridable" without any warning, but I don't find any options to avoid that.

I can also create setters and getters, but the same applies.

Answer

It sounds like you would like to have final methods (methods that subclasses are forbidden from overriding). This feature has been requested of TypeScript, but as of the time of writing: it's not being accepted.