MediumJavaScriptTypeScript

Mapped Types — Nullable & Optional

TypeScriptMapped TypesUtility Types

Implement two type-level utilities and their runtime equivalents:

1. `makeNullable<T>` — every value in the object can also be null.
Runtime: toNullable(obj) returns the same object (values unchanged — the type is the change).

2. `pickKeys<T>(obj: T, keys: (keyof T)[]): Pick<T, ...>` — returns a new object with only the specified keys.

Examples

  • pickKeys({ a: 1, b: 2, c: 3 }, ['a', 'c']){ a: 1, c: 3 }
  • pickKeys({ x: 'hello' }, []){}

Sample tests

Test #1pick a and c
Input: ["pickKeys",{"a":1,"b":2,"c":3},["a","c"]]
Output: {"a":1,"c":3}
Test #2empty keys returns empty object
Input: ["pickKeys",{"x":"hello"},[]]
Output: {}
Test #3pick single key
Input: ["pickKeys",{"a":1,"b":2},["b"]]
Output: {"b":2}