Skip to content

Instantly share code, notes, and snippets.

@jfet97
Last active April 18, 2022 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfet97/9a081c1356c694f2ef51925e2376e083 to your computer and use it in GitHub Desktop.
Save jfet97/9a081c1356c694f2ef51925e2376e083 to your computer and use it in GitHub Desktop.
Mapped types distribute as well
type Nodes =
| {
type: "a";
age: number;
}
| {
type: "b";
size: string;
};
type Validation<T> = {
// T is naked
[P in keyof T]: boolean;
};
type R = Validation<Nodes>;
// Validation<{ type: "a"; age: number; }> | Validation<{ type: "b"; size: string; }>
//
// Nodes is an union of types && mapped type is homomorphic
// => the mapped type distributes over the constituents of the union
// => the result is the union of the resulting mapped types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment