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

issues#179 d.ts type definition error #181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
49 changes: 47 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
declare function deepmerge<T>(x: Partial<T>, y: Partial<T>, options?: deepmerge.Options): T;
declare function deepmerge<T1, T2>(x: Partial<T1>, y: Partial<T2>, options?: deepmerge.Options): T1 & T2;
/**
* object skin copy
* e.g: SkinMerge<{ a: 1, c: 1 }, { a: 2, b: 2 }> ==> { a: 2, b: 2, c: 1 }
*/
type SkinMerge<O1, O2> = {
[k in (keyof O1 | keyof O2)]: k extends keyof O2 ? O2[k] : (k extends keyof O1 ? O1[k] : never)
}

/**
* get keys from O where the value is object
* e.g: ObjKeys<{ a: {}, b: {}, c: '1' }> ==> 'a' | 'b'
*/
type ObjKeys<O> = ({
[k in keyof O]: O[k] extends object ? k : never;
})[keyof O];

/**
* contrary to ObjKeys, e.g: NotObjKeys<{ a: {}, b: {}, c: '1' }> ==> 'c'
*/
type NotObjKeys<O> = Exclude<keyof O, ObjKeys<O>>;

/**
* use NotObjKeys<O> to contruct object type from 'O'
* e.g: PickNotObj<{ a: 'a', b: 'b', c: {} }> ==> { a: 'a', b: 'b' }
*/
type PickNotObj<O> = Pick<O, NotObjKeys<O>>;

/**
* Merge Deep Recursively For O1 O2
* github gist about MergeDeep: https://gist.github.com/eczn/f037be1fa3650304c5c4b80c3b9e18a7
*/
type MergeDeep<O1, O2> = SkinMerge<PickNotObj<O1>, PickNotObj<O2>> & {
// k1 is not in ObjKeys<O2>
[k1 in Exclude<ObjKeys<O1>, ObjKeys<O2>>]: O1[k1]
} & {
// k2 is ObjKeys<O2>, maybe some ObjKeys<k1> alson in there, so we should find it out
[k2 in ObjKeys<O2>]: k2 extends ObjKeys<O1> ? MergeDeep<O1[k2], O2[k2]> : O2[k2]
}

/**
* ## JS Object Deep Merge
* look more info in https://github.com/TehShrike/deepmerge
* @param x the first obj
* @param y the second obj
* @param options deepmerge option
*/
declare function deepmerge<T1 extends object, T2 extends object>(x: T1, y: T2, options?: deepmerge.Options): MergeDeep<T1, T2>

declare namespace deepmerge {
export interface Options {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"rollup-plugin-node-resolve": "^5.2.0",
"tape": "^4.11.0",
"ts-node": "7.0.1",
"typescript": "=2.2.2",
"typescript": "^3.7.2",
"uglify-js": "^3.6.1"
},
"license": "MIT",
Expand Down