cspotcode / cspotcode/typescript-toolbelt
Add `UnionToIntersection` and maybe `MustBeNullable<T>`
- Dominant language
- TypeScript
- Stars
- 19
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
```
type UnionToIntersection =
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
```
^--- how it works: splat parts of the union into a union of functions whose first arg is the type.
Then infer type of that first arg. It'll be an intersection, because first arg must satisfy *all* parts of the union in order to safely invoke the function.
```
/** Evaluates to `never` if T is not nullable; otherwise evaluates to T */
type Nullable = UnionToIntersection extends (T & undefined) | (T & null) | (T & null & undefined) ? T : never;
```
Should this be a conditional? `IfNullable`
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.