4 ms·
Be careful though, as they are not type safe in the sense the compiler completely trusts the type guard is implemented correctly. In other words, TypeScript ha
by city41 8y ago
Be careful though, as they are not type safe in the sense the compiler completely trusts the type guard is implemented correctly.
In other words, TypeScript has no problem with this:
function isNumber(a: any): a is number {
return typeof a === 'string';
}
Type guards are usually simple functions, but I've gotten them wrong before and only found out with a runtime exception.
- benschulz 8y agoYes, I really wish that simple arrow functions were inferred for that reason. I've implemented a proof-of-concept[1], but I don't see it being added to the compiler any time soon. It's a shame because it would make a lot of my code more robust. [1]: https://github.com/Microsoft/TypeScript/issues/10734#issuecomment-351570480 https://github.com/Microsoft/TypeScript/issues/10734#issueco...