Skip to content

Instantly share code, notes, and snippets.

@joshwashywash
Last active February 13, 2023 18:23
Show Gist options
  • Save joshwashywash/32d2f34ca29c29d5a85f8554b2af5814 to your computer and use it in GitHub Desktop.
Save joshwashywash/32d2f34ca29c29d5a85f8554b2af5814 to your computer and use it in GitHub Desktop.
gets keys of an object in typescript
const typedKeys = <O extends {}>(o: O) =>
	Object.keys(o) as Array<keyof O>;

const o = {
	first: 'josh',
	last: 'o'
};

const keys = Object.keys(o);

type T = typeof keys[number] // string

const betterKeys = typedKeys(o);

type U = typeof betterKeys[number]; // 'first' | 'last'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment