Skip to content

Instantly share code, notes, and snippets.

@roboli
Created April 9, 2024 11:33
Show Gist options
  • Save roboli/d8fd5b5885a4708c0d1b5138da124b92 to your computer and use it in GitHub Desktop.
Save roboli/d8fd5b5885a4708c0d1b5138da124b92 to your computer and use it in GitHub Desktop.
Using Object.keys(obj) in Typescript
function objectKeys<T>(obj: T): (keyof T)[] {
return Object.keys(obj) as (keyof T)[];
}
// ...
// Avoid the array of strings coming from Object.keys in TS source https://twitter.com/mattpocockuk/status/1502264005251018754?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E1502264005251018754%7Ctwgr%5E1d1ec82885093024cd07b1d7a4a1d21f4e095a76%7Ctwcon%5Es1_c10&ref_url=https%3A%2F%2Fwww.mattstobbs.com%2Fobject-keys-typescript%2F
// objectKeys(myObj).find( ... );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment