dan-silver-microsoft dan-silver-microsoft - 20 days ago 6x
TypeScript Question

Can TypeScript classes remove access to inherited methods?

Classes can add methods when they extend from a super class, but can they do the opposite and remove methods? The goal is for these removed classes to not be accessible at compile time. I'm essentially trying to get (new car()). to not autocomplete .drive() in an IDE.

class vehicle {
drive() {

class car extends vehicle {
//previous attempt
private drive() { // can't modify public/private



Is there a way at compile time (tsc) to prevent car from having the method drive? Can we either delete it or modify it's accessibility?


No. As noted in comments, this is indicative that your hierarchy is not correct. A derived class must always be substitutable for its base class.

You have to consider what happens when someone writes code like this

function doSomething(y: Vehicle) {
let x = new Car();