ed'
ed'

Reputation: 1895

Typescript interface generics are type checked but not inferred

In the sample code below, when implementing MyInterface...

If it can detect an incorrect type, why can it not infer the correct type?

interface MyInterface<T = any> {
    myFunction(input: T): void;
}

class MyClass implements MyInterface<number> {
    // input inferred as 'any'
    myFunction(input) {}
}

class MyClass2 implements MyInterface<number> {
    // Type 'number' is not assignable to type 'string'
    myFunction(input: string) {}
}

Upvotes: 0

Views: 25

Answers (1)

Matt McCutchen
Matt McCutchen

Reputation: 30909

This is a little more complicated than it looks in general. There is an open suggestion.

Upvotes: 1

Related Questions