4 ms·
any is assignable to anything so what you’re seeing here is the expected behavior. What I’d recommend is not to use any (and enable the linter rules to disable
by n_e 7y ago
any is assignable to anything so what you’re seeing here is the expected behavior.
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.
- chupa-chups 7y agoThanks for the reply. I'm writing a ts parser and stumbled over this. 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).
- sbergot 7y agoThe reason is that any is an escape mechanism. Typescript will never complain about anything when dealing with this type. Its use case is to perform a step by step migrations from js or to deal with rare corner cases when you can prove yourself that the code is correct.