3 ms·
> Why does 0 | 1 extends 0 ? true : false evaluate to false? Because 'extends' really means 'is assignable to'. I feel like most of the questions from the pos
by scotty79 4y ago
> Why does 0 | 1 extends 0 ? true : false evaluate to false?
Because 'extends' really means 'is assignable to'.
I feel like most of the questions from the post might be answered fairly easily by using reasoning of 'is assignable to'.
Things assignable to A&B are things assignable to both A and B.
Things assignable to A|B are things assignable to A or to B.
'never' means a type that nothing is assignable to and is assignable to everything (bit weird because it usually doesn't have any values but you can create value of type never with the use of 'as').
'unknown' means a type that everything can be assigned to but it's not assignable to anything (except itself and any).
'any' means a type that everything is assignable to and that is assignable to everything (except never).
Ok. Ok. {} types is bit weird way to denote interfaces. :-)
- thoughtspile 4y agoFair enough, "is assignable to" is another synonym for "is subset of". I find it much easier to reason about things I can visualize, like sets, which is why I love my set interpretation. Edit: besides, it's quite unintuitve that "never" is assignable to anything. How can never be something?
- scotty79 4y agoIt's harder for me to make that connection but thank you for your write up. It's really great.
- Danoha 4y agoBecause "never" is an empty set and empty set is a subset of all sets.
- thoughtspile 4y agoExactly, that's why I wrote the article =) Makes perfect sense in "set world", but not in "common sense" based on your feeling of the word "never"
- scotty79 4y ago> it's quite unintuitve that "never" is assignable to anything Yeah, at first I was mislead by this. Initially I thought never is opposite of any while, it turns out, it's opposite of unknown. And any is just super weird, dynamic wildcard thing that is as large or as small as needed for any given operation (but not as small as never).
- lmm 4y ago> Fair enough, "is assignable to" is another synonym for "is subset of". Only if you're happy to conflate members and sets, which is pretty confusing IMO. If I write a = b I don't generally think of b as being a set, even though in some sense it is; "b is an expression with this type" is a much more natural way of thinking to me.
- LudwigNagasena 4y agoYeah, `extends` seems like a misnomer that confuses people.