I'm playing around with https://2ality.com/2020/06/computing-with-types.html#conditional-types to learn the type relationships in TypeScript.
Having defined the type function IsAssignableTo:
type IsAssignableTo<A, B> = A extends B ? true : false
I got the desired results like:
type MyGoodType1 = IsAssignableTo<42, Object> // MyGoodType1 === true
type MyGoodType2 = IsAssignableTo<42, object> // MyGoodType2 === false
type MyGoodType3 = IsAssignableTo<'foo', 'foo'|'bar'> // MyGoodType3 === true
However, the result of reversing the parameters of the last expression:
type MyStrangeType = IsAssignableTo<'foo'|'bar', 'foo'> // MyStrangeType === boolean ???
is, to my surprise, neither literal true nor literal false (expecting it to be false), but a boolean?
My Questions are
- How assignability for union types works in TS? I've read SPEC's Assignment Compatibility and got no luck.
- How could the result of
T1 extends T2 ? A : Bbecome a union ofAandB?