Earlier quoted context omitted.
Typescript is actually a great language. And with those utility types, you can do pretty fun stuff like, for example, you want to mutate a type so that some fields become mandatory: type Ensure = T & { [U in keyof Pick ]-?: T[U] }; class A { foo?: number; bar?: number; baz?: number; } type MandatoryFields = "foo" | "baz"; type B = Ensure ; const b: B = { foo: 42 }; Here, ts will complain that b is missing baz.
This seems extremely hard to read for me, I would have an hard time trying to understand what it does if I found it in any source code
type Ensure
I define a type called Ensure
This type takes two type parameters, one called T and the other K which will consist of Keys belonging to the type T (in our case, "foo", "bar" or "baz"). = T &
This new type (called Ensure) will be equal to the union of two types: One will be T and the other will be: { [U in keyof Pick]
A new type which keys will be picked among the key listed in K -?
To which we will remove the potential optional qualifier : T[U] };
And which types will be the same as in T.