HardJavaScriptTypeScript

pick & omit (typed utilities)

TypeScriptFunctionsPatterns

Implement two of the most-used object utilities. Both should be **shallow,
immutable** transforms — never mutate the input.

pick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>
omit<T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>

Rules

  • pick returns a new object containing only the listed own keys (skip keys

not present on obj).

  • omit returns a new object containing every own key except the listed

ones.

  • Inherited properties must be ignored.

In TypeScript, the real challenge is the type signature — make sure your
return type is narrowed via Pick / Omit so callers keep full inference.

Sample tests

Test #1pick selects listed keys
Input: ["pick",{"a":1,"b":2,"c":3},["a","c"]]
Output: {"a":1,"c":3}
Test #2omit excludes listed keys
Input: ["omit",{"a":1,"b":2,"c":3},["b"]]
Output: {"a":1,"c":3}
Test #3pick ignores keys not on the source
Input: ["pick",{"a":1,"b":2},["a","missing"]]
Output: {"a":1}
Test #4pick with no keys returns empty object
Input: ["pick",{"a":1,"b":2},[]]
Output: {}
Test #5omit-everything returns empty object
Input: ["omit",{"a":1,"b":2,"c":3},["a","b","c"]]
Output: {}