5 points · by chupa-chups · 7y ago · 8 comments
type sig = (p: number) => string;
const x = (p: any) => p + 1;
const y: sig = x;
alert(typeof y(5));
Obviously the alert message will (and does) display 'number'.IntelliJ spots the problem though ('x' should probably not be assigned to 'y').
(Apologies if this is too stackoverflow-y, but I don't expect a "solution", I'm just interested in opinions)
What I’d recommend is not to use any (and enable the linter rules to disable it) and rather use unknown (which is assignable to nothing) or generics.
On a more general note, typescript is not sound (though it works well in practice) and you’ll always be able to find incorrect code that typechecks, even without using any.
The reason I find it strange is that in this case it appears detectable that the return type will not match (independent of the any type in the parameter).
Is there a reason not to check this I'm not seeing?
By the way, I am quite amazed at the quality (and depth) of the typescript type inference & check model (despite it being "not sound" to quote you).
Another thing you can try is to pass x in as a parameter to a function. I would expect that this would not compile -- because the compiler can't guarantee (without jumping through incredible hoops) that x is always going to use p as a number. If it does compile, try to pass 2 functions: one numerical and one that uses chars. If that compiles, then I think you have a legitimate error in the type checker. Otherwise I think it's just fine.