Typescript abstract method doesn't inherit the params type

Viewed 197

I have the following code:

abstract class Foo<T extends { data: string }> {
  abstract doSomething(params: T): void;
} 


class Baz extends Foo<{ id: string; data: string }> {
   doSomething(params) {}
}

My expectation is that when implementing the doSomething method it'll automatically infer the params as T, but it infers it as any. Am I wrong?

1 Answers

By extending, the compiler just checks whether the derived class is assignable to the base class, you are free to design the derived class as long as it is assignable to the base class.

Imagine you are not extending, just writing a regular class. doSomething(param) does implicitly means doSomething(param:any). This implication dose not change even when you are extending. Since (any) => void is assignable to ({ id: string; data: string }) => void , everything is fine. The compiler won't do extra work.

Related