Skip to content

Commit

Permalink
kerosene: Add DistributiveOmit, DistributivePick types (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhardy committed Sep 5, 2022
1 parent 850630c commit de6cd80
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/kerosene-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "@kablamo/kerosene-ui",
"version": "0.0.25",
"version": "0.0.26",
"repository": "https://github.com/KablamoOSS/kerosene/tree/master/packages/kerosene-ui",
"bugs": {
"url": "https://github.com/KablamoOSS/kerosene/issues"
Expand All @@ -22,7 +22,7 @@
},
"dependencies": {
"@babel/runtime": "^7.18.9",
"@kablamo/kerosene": "^0.0.25",
"@kablamo/kerosene": "^0.0.26",
"@types/lodash": "^4.14.184",
"lodash": "^4.17.21"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/kerosene/package.json
@@ -1,6 +1,6 @@
{
"name": "@kablamo/kerosene",
"version": "0.0.25",
"version": "0.0.26",
"repository": "https://github.com/KablamoOSS/kerosene/tree/master/packages/kerosene",
"bugs": {
"url": "https://github.com/KablamoOSS/kerosene/issues"
Expand Down
8 changes: 8 additions & 0 deletions packages/kerosene/readme.md
Expand Up @@ -238,6 +238,14 @@ Infers the element type `T` from `T[]`, `Set<T>`, `Map<any, T>`, or `{ [key: str

Infers the union of all object entry tuples for type `T`.

### `DistributiveOmit<T, K>`

Like `Omit<T, K>`, but distributes across all members of a union.

### `DistributivePick<T, K>`

Like `Pick<T, K>`, but distributes across all members of a union.

### `KeysOfUnion<T>`

Like `keyof T`, but distributes across all members of unions to include all keys (including those not shared by all members).
Expand Down
25 changes: 25 additions & 0 deletions packages/kerosene/src/types/index.ts
Expand Up @@ -35,6 +35,31 @@ export type ElementType<TCollection> =
*/
export type Entries<T> = { [P in keyof T]: [P, T[P]] }[keyof T];

/**
* Like `Omit<T, K>`, but distributes across all members of a union.
*
* e.g.
* ```typescript
* type Base = Omit<{ base: string } & ({ a: string } | { b: string; }) & { common: string }, "common">;
* // equivalent to (note that a and b are missing)
* type Base = { base: string };
*
* type AorB = DistributiveOmit<{ base: string } & ({ base: string; a: string } | { base: string; b: string; }) & { common: strubg }, "common">;
* // equivalent to (note that a and b are present)
* type AorB = { base: string } & ({ a: string } | { b: string });
* ```
*/
export type DistributiveOmit<T, K extends PropertyKey> = T extends any
? Omit<T, K>
: never;

/**
* Like `Pick<T, K>`, but distributes across all memebrs of a union.
*/
export type DistributivePick<T, K extends KeysOfUnion<T>> = T extends any
? Pick<T, K>
: never;

/**
* Like `keyof T`, but distributes across all members of unions to include all keys (including those
* not shared by all members)
Expand Down

0 comments on commit de6cd80

Please sign in to comment.