keyof typeof

Published: September 11, 2022

With keyof typeof we can get a union type with all the property names of an object.

ts
const me = { name: 'Gaute', age: 31 }
type Keys = keyof typeof me // "name" | "age"

Explanation ​

We know typeof from JavaScript, but TypeScript gives a bit more info than just object.

ts
type MeType = typeof me
/*
{
    name: string;
    age: number;
}
*/

And then keyof on a type will give us all the keys/property names in a union type, so a value for keyof typeof me has to be either name or age.