Skip to content

Instantly share code, notes, and snippets.

@mikob
Last active June 28, 2023 07:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikob/037582ddd303af472ada4bacd01d05d3 to your computer and use it in GitHub Desktop.
Save mikob/037582ddd303af472ada4bacd01d05d3 to your computer and use it in GitHub Desktop.
TypeScript pick overlapping properties
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