# TypeScript's keyof and Mapped Types

1 min read

The keyof operator and mapped types in TypeScript allow for advanced type manipulation. Here’s an example:

interface User {
id: number
name: string
email: string
}
type UserKeys = keyof User // 'id' | 'name' | 'email'
type ReadonlyUser = {
[K in keyof User]: Readonly<User[K]>
}
const user: ReadonlyUser = {
id: 1,
name: 'John',
email: 'john@example.com',
}
// user.id = 2; // Error: Cannot assign to 'id' because it is a read-only property.

These features make TypeScript a powerful tool for creating robust and type-safe applications.

Exploring keyof and Mapped Types
echo "Using keyof and mapped types in TypeScript"
Fractal Tess avatar

Thanks for reading! Perhaps you’d be interested in one of the things below?


More Posts