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 -
Maybe this is because “unknown” came out after type predicates so the purpose was originally to pass in “any” and get back a type. But with “unknown”, that’s kinda a better way to runtime check the type. Cc
@drosenwasser@SeaRyanC1 reply 0 retweets 1 like
unknown refinements are even more finnicky though. Type predicates are useful to abstract out refinement logic.
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 