Last active
June 3, 2020 23:27
-
-
Save cdmcmahon/5f3aacb4148cfa0c03d70926b8858938 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given this passes the type checker | |
type A = 'a' | 'b' | 'c'; | |
type ShouldBeTrue = A extends string ? true : false; | |
const test: ShouldBeTrue = true; | |
// And these types | |
type FooBar<T = string | { [key: string]: FooBar<string> }> = T extends string | |
? Bar<T> | |
: { | |
[K in keyof T]: FooBar<T[K]>; | |
}; | |
interface Bar<T> { | |
f(t: T): void; | |
} | |
// Why areen't B and C the same? | |
type B = Bar<A>; // Bar<'a' | 'b' | 'c'> | |
type C = FooBar<A>; // Bar<'a'> | Bar<'b'> | Bar<'c'> | |
// The distinction is important because the functions are no longer compatible | |
type BF = B['f']; // (t: A) => void | |
type CF = C['f']; // ((t: "a") => void) | ((t: "b") => void) | ((t: "c") => void) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment