Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Owner

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?

*
* 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>`
Copy link
Owner

Choose a reason for hiding this comment

The 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 {}> = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use more descriptive type names than T and V? See the other types for inspiration.

[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}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this different from Merge?

* @example `const a: Spread<X, Y> = { ...x, ...y }`
Copy link
Owner

Choose a reason for hiding this comment

The 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;