0

I've got the following TypeScript classes:

class AnyBackendRoute {
   public getProps() {...}
}
class AnyBFFRoute extends AnyBackendRoute {
   public getProps() {...}
}
class UserSignUpRoute extends AnyBFFRoute {
   public getSomethingElse() {...}
}

I transpile them to commonJS-ES6 and include them in another file (.js).

In the original code (in test file, before transpilation) I can easily do:

const route = new UserSignUpRoute()

// getProps() visible from AnyBFFRoute as it's a public method
console.log(route.getProps())

but when I try the same thing with the transpiled code (that contains also .d.ts file), I am getting a TypeScript error:

Type 'UserSignUpRoute' is missing the following properties from type 'AnyBFFRoute': getProps

In the transpiled code I would expect to find something like:

UserSignUpRoute.prototype.getProps = function () {
    return this.props;
};

but it's not there! Same, in the appropriate .d.ts file. I cannot see getProps() method from the parent class but only the local public methods.

Am I wrong to think those methods should be available in the UserSignUpRoute.js as well?

If so, why there is an inconsistency so that I can call route.getProps() in the pre-transpiled code and not afterwards?

Greg Wozniak
  • 5,209
  • 3
  • 26
  • 29
  • 1
    Here are links to a few interesting reading material on this: https://stackoverflow.com/questions/43595943/why-are-derived-class-property-values-not-seen-in-the-base-class-constructor https://github.com/microsoft/TypeScript/issues/1617 – Phanisimha N R Mar 20 '23 at 22:05
  • 1
    `UserSignUpRoute.prototype.getProps` would only be there if you're targetting pre-ES2015. – evolutionxbox Mar 20 '23 at 22:12
  • so would it be a good practice to repeat: `public getProps() {return super.getProps()}` to achieve the visibility? – Greg Wozniak Mar 21 '23 at 08:59

0 Answers0