Dev Dastan: post #46 — TG.ME

🟦 TypeScript Tips and Tricks Series (Part 4)

❤ Previous Parts: [part 1 | Part 2 | Part 3]

🔣 Mapped Types

Transform existing types by applying operations to each property. Useful for adding prefixes/suffixes to properties, creating key-value pairs from objects, etc.


interface User {
name: string;
age: number;
}

type ReadOnly<T> = { readonly [P in keyof T]: T[P] };

const readOnlyUser: ReadOnly<User> = {
name: 'Kurt',
age: 27
};

// readOnlyUser.name = 'Chester';
// Error: Cannot assign to 'name' because it is a read-only property


✨ Use Case:
Useful for manipulate and transform data from the API or before passing data to another component, with different data types.


#TypeScript


🖥 Follow @devDastan for more content.
👍11❤2🤯1
May 31, 2024 668 1