So I have the following code sample:
interface MyInterface<T> {
myFunc(value: T): void;
}
class MyImplementation implements MyInterface<number> {
myFunc(value: number): void {
console.log(value / 2);
}
}
function myTest(): MyInterface<number|string> {
return new MyImplementation(); // doesn't look quite right
}
myTest().myFunc("I am not a number"); // outputs NaN
I can't quite get my head around why typescript is allowing me to return MyImplementation in place of MyInterface<number | string>. I understand we want number to be assignable to number | string but surely not for generic parameters.