Mapped type with template literals
📖 Scenario: You are working on a user profile system where you want to create new property names by adding prefixes to existing keys.
🎯 Goal: Create a mapped type that uses template literals to add a prefix to each key of an existing interface.
📋 What You'll Learn
Create an interface called
User with keys name and age.Create a type called
PrefixedUser that adds the prefix user_ to each key of User using a mapped type with template literals.Create a variable
prefixedUser of type PrefixedUser with appropriate values.Print the
prefixedUser object.💡 Why This Matters
🌍 Real World
Adding prefixes or suffixes to keys is useful when integrating with APIs or libraries that require specific naming conventions.
💼 Career
Understanding mapped types and template literals helps in writing flexible and reusable TypeScript code, a valuable skill for frontend and backend developers.
Progress0 / 4 steps