# TypeScript's keyof and Mapped Types

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"
My avatar

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.


More Posts