Last active
June 28, 2023 07:11
-
-
Save mikob/037582ddd303af472ada4bacd01d05d3 to your computer and use it in GitHub Desktop.
TypeScript pick overlapping properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Overlap<T, U> = { [K in keyof T & keyof U]: U[K] }; | |
/** | |
* Return an object with only the properties existing on both objects, values equal to what's on the latter object. | |
*/ | |
export function pickOverlapping<T, U>(obj1: T, obj2: U): Overlap<T, U> { | |
const ret: any = {}; | |
for (const k in obj2) { | |
if (k in obj1) { | |
ret[k] = obj2[k]; | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment