D
D
Dmitry Ermolaev2020-01-30 19:00:18
typescript
Dmitry Ermolaev, 2020-01-30 19:00:18

How to correctly describe the interface for checking method arguments in TS?

There is an interface IFoo and a class Foo

interface IFoo {
  bar(value: number): number;
}

class Foo implements IFoo {
  bar(num) {
    return num * num;
  }
}


Despite the fact that both the method and its argument are described in the interface, nevertheless, the num argument is highlighted as any

Playground where you can see the actual example

Question: how to correctly describe the interface so that the argument is highlighted correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
XaveScor, 2020-01-31
@iusfof

No way. Unfortunately, you must explicitly declare the types of the arguments. Ts can only help you if the type in the class is incompatible with the type in the interface.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question