Evert
Evert

Reputation: 99816

Let typescript infer the type of of a class method, implementing an interface

In our application, I would like it to be optional to have to define method arguments, if those are known from a parent:

interface Parent<T> {

    foo(arg1: T): void;

}

class MyImpl implements Parent<string> {
    foo(arg1) {

    }
}

Currently this throws an error:

Parameter 'arg1' implicitly has an 'any' type.

I'm curious if there's a way to avoid this error.

The real-world use-case is a lot more complex, and making the argument type optional avoids a bunch of quite complicated type juggling.

There's other places in Typescript where it can infer, specifically:

type Callback = (s:string) => void;

function foo(cb: Callback) {
  cb('hi');
}

foo(arg => console.log(arg));

I'm hoping I can make this somehow work with subclasses.

Upvotes: 0

Views: 443

Answers (1)

Tal Ohana
Tal Ohana

Reputation: 1138

Unfortunately it is not possible. There has been a discussion about it in this issue - https://github.com/Microsoft/TypeScript/issues/1373

Upvotes: 2

Related Questions