Automatically infer types from overridden interfaces in TypeScript

I am trying to create some TypeScript definitions for already existing modules. In the specific interface to be implemented, the signature looks like this:

type NextFunction<T> = () => T;
type Response = string[] | Promise<string[]>;

interface IPage {
  getBodyClasses(next: NextFunction<Response>): Response;
}

      

The parameter and return structures are fixed, and I really would like to have TypeScript inference as to what types of parameters are in my overridden methods. However, when I create my override, I see that the parameter is implicitly of type any

.

class Page implements IPage {
  getBodyClasses(next) {
    return next();
  }
}

      

Is it possible to mark getBodyClasses

as highlighted override so that parameter types are automatically inferred? It would have already been said that I Page

missed the interface if I typed next

like number

, so I don't quite understand why it also cannot infer that the type is the next

same as the type of the interface.

+3


source to share


1 answer


Context typing of implemented properties is not supported.

More details



The main issue that tracks this is https://github.com/Microsoft/TypeScript/issues/1373

+1


source







All Articles