After using TypeScript there's some features that I'm terrified of that seem counter intuitive. All the following examples produce NO errors and are with every single strict TS option enabled.
-
Show this thread
-
Type predicates are just an annotation and aren't actually checked. Meaning you can do some pretty wacky and unsafe things. Is there an technical constraint preventing TS from checking these?pic.twitter.com/qtIIzM1Cat
9 replies 1 retweet 17 likesShow this thread -
Replying to @sebmck
I use type predicates about as much as type casting: rarely. They’re both powerful yet unsafe. Occasionally you need to tell TS that you know better than the type system.
1 reply 0 retweets 2 likes -
Replying to @styfle
I have 12 uses of type predicates and 81 uses of "type casting". They would be even more powerful if they were safe.
2 replies 0 retweets 1 like -
Replying to @sebmck
If your 12 uses of type predicates is similar to your example of a string primitive, it’s actually safer to move the typeof inline as a type guard. The predicates are only useful for nested objects. It would be great if they were safe and forced an explicit cast to be unsafe.
2 replies 0 retweets 1 like -
Replying to @styfle
Here's a few instances of my usages of type predicates. It's not really an option to inline all of these, especially `isStatement`. They're essential for refinements of very large unions.pic.twitter.com/XC2FWCZls0
1 reply 0 retweets 2 likes -
Replying to @sebmck
The second two look fine but the isStatement can probably be achieved by using discriminated unions. If you have a switch like that, seems like you are using one property to determine the type of the object. https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions …
1 reply 0 retweets 2 likes
I left out all the other cases, there's about 50 other possible type properties that it can match.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
he/him 