SyntaxStudy
Sign Up
Home TypeScript Reference typeof / keyof / typeof

typeof / keyof / typeof

class

keyof produces a union of a type's property names; typeof gets the type of a variable.

Syntax

keyof Type typeof variable

Example

javascript
interface User { id: number; name: string; email: string; }

type UserKey = keyof User; // 'id' | 'name' | 'email'

function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
    return obj[key];
}

// typeof
const config = { host: 'localhost', port: 3000 };
type Config = typeof config;
// { host: string; port: number }