-
-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Diff
and Spread
types
#7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,3 +84,23 @@ const ab: Merge<Foo, Bar> = {a: 1, b: 2}; | |
``` | ||
*/ | ||
export type Merge<FirstType, SecondType> = Omit<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType; | ||
|
||
/** | ||
* Diffs two objects. | ||
* | ||
* Given objects with types T and V, returns an object that has all the keys in T that do not also exist in V. | ||
* | ||
* @example `type Safe = Diff<AllProperties, UnsafeProperties>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you include a more comprehensive example? Should also include the import statement. |
||
*/ | ||
export type Diff<T extends {}, V extends {}> = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use more descriptive type names than |
||
[P in Exclude<keyof T, keyof V>]: T[P]; | ||
}; | ||
|
||
|
||
/** | ||
* Returns a type modeling the result of spreading two objects together. | ||
* | ||
* Given objects with types T and V, returns an object that has a type that represents {...T, ...V} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this different from |
||
* @example `const a: Spread<X, Y> = { ...x, ...y }` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't TS already infer the new type correctly from object-spread? Maybe I'm missing the use-case for this type. |
||
*/ | ||
export type Spread<T extends {}, V extends {}> = Diff<T, V> & V; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add this to the readme too?