Having an issue with my TypeScript 3.4 code that seems a bit strange. Here's a snippet of the problematic code:
interface MyInterface {
fn: (x: number) => number;
}
abstract class A {
abstract prop: MyInterface;
}
class B extends A {
prop = { fn: x => x }; // Receiving complaints about implicit any with 'x'
}
When defining class B as shown below, the complaint disappears:
class B extends A {
prop: MyInterface = { fn: x => x }; // No more complaints!
}
Seems like TypeScript is having trouble understanding the function parameter x
. Normally, TypeScript correctly identifies type MyInterface
for prop
, but not in this case when it comes to functions.
Can someone shed some light on what I might be doing wrong? This example captures the essence of the issue I'm facing. In my actual project, things are organized into different files and I have to import MyInterface
for each implementation of the abstract class, even though it should be inferred automatically.