I have a tiny code implementation in Typescript where I have a class either implementing an interface or extending a class.
interface ITest {
run(id: number): void
}
abstract class Test implements ITest {
abstract run(id);
}
class TestExtension extends Test
{
constructor() {
super();
}
public run(id) { }
}
class TestImplementation implements ITest {
constructor() {
}
public run(id) { }
}
Both shows wrong Intellisense where I expected id to by of type 'number':
(method) TestExtension.run(id: any): void
(method) TestImplementation.run(id: any): void
I can of course set method implementation to be public(id: number) { } but I don't see why I have to do this.
Can someone enlighten me, please?
Your understanding of
implements ITestis a bit in correct. TypeScript interface implementation is not like other langauges, it is purely a compile time contract static that members of the class must be compatible with the interface definition.So like the following is correct:
the following is also correct
The function argument is also any in your code without an explicit signature
Fix
Be explicit:
public run(id) { }topublic run(id: number) { }And optionally compile withnoImplicitAnyif you want the compiler to catch these cases for you