Skip to content

CodeNot attempted

Typed getProperty, update and pair

Difficulty: easy

Make these correct at runtime and precisely typed, with no any in the signatures:

  • getProperty(obj, key): the key must exist on obj, and the return type is that property's type (T[K])
  • update(original, changes): changes can only contain known keys with the right types (Partial<T>)
  • pair(a, b): returns a typed tuple [A, B]
Show reference solution
export function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
export function update<T extends object>(original: T, changes: Partial<T>): T {
return { ...original, ...changes };
}
export function pair<A, B>(a: A, b: B): [A, B] {
return [a, b];
}
Loading editor…