Sobes.tech
Junior — Senior

Type-safe access to object properties through a function

livecode

Task condition

It is necessary to create a function that takes an object and a string key, and at compile time, it should check whether the key belongs to this object. When attempting to call the function with a key that does not exist in the object, the compiler should generate an error.

const X = { a: 1, b: 2, c: 3, d: 4 };

function getProperty(obj, key) {
  return obj[key];
}

getProperty(X, 'a');
getProperty(X, 'm'); //error