Skip to content

Commit 0f56a7f

Browse files
authored
Adds Diff<> and Omit<> generics
1 parent 1ecc8ec commit 0f56a7f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,13 @@ function narrowLiterals(array) {return array;}
5959
function typeOfExpression<T>(fn: (_?: any) => T): T;
6060
function typeOfExpression() {};
6161
```
62+
63+
/*
64+
* Copied from http://ideasintosoftware.com/typescript-advanced-tricks/
65+
* Diff<StringUnion, StringUnionToSubtract> is a string literal union of all the values in StringUnion that do not appear in StringUnionToSubtract
66+
* Omit<T, KeysToOmit> is type T but without all the properties named by KeysToOmit.
67+
* My addition, OmitFromInterface<T, U> is like Omit except it omits the *properties* of the second type, so U is an interface as well.
68+
*/
69+
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
70+
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]};
71+
type OmitInterface<T, U> = Omit<T, keyof U>;

0 commit comments

Comments
 (0)