Skip to content

Instantly share code, notes, and snippets.

@cdmcmahon
Last active June 3, 2020 23:27
Show Gist options
  • Save cdmcmahon/5f3aacb4148cfa0c03d70926b8858938 to your computer and use it in GitHub Desktop.
Save cdmcmahon/5f3aacb4148cfa0c03d70926b8858938 to your computer and use it in GitHub Desktop.
// 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