Skip to content

Instantly share code, notes, and snippets.

@wojtekKrol
Created January 11, 2024 14:39
Show Gist options
  • Save wojtekKrol/6ecd128db239cf1972b9f49315d29b3d to your computer and use it in GitHub Desktop.
Save wojtekKrol/6ecd128db239cf1972b9f49315d29b3d to your computer and use it in GitHub Desktop.
TypeScript utility for type-safe dynamic key lookup
export const myObject = {
a: 1,
b: 2,
c: 3,
};
const ObjectKeys = <Obj extends Object>(obj: Obj): (keyof Obj)[] => {
return Object.keys(obj) as (keyof Obj)[];
};
ObjectKeys(myObject).map(key => {
return myObject[key]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment