A mapped type creates a new type by transforming each property of an existing type. It loops over keys and applies changes, like making properties optional or readonly.
Click to reveal answer
beginner
What are template literal types in TypeScript?
Template literal types let you create new string types by combining or transforming other string types, similar to string templates but at the type level.
Click to reveal answer
intermediate
How do mapped types and template literal types work together?
You can use template literal types inside mapped types to create new property names by combining or modifying existing keys, like adding prefixes or suffixes.
Click to reveal answer
intermediate
Example: What does this mapped type do?
type PrefixedKeys<T> = { [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K] }
It creates a new type where each property name is prefixed with 'get' and capitalized, and the value is a function returning the original property type.
Click to reveal answer
beginner
Why use mapped types with template literals?
They help automate creating new types with modified keys, reducing repetitive code and improving type safety by generating consistent property names.
Click to reveal answer
What does the 'as' keyword do in a mapped type with template literals?
AIt filters keys from the original type
BIt changes the value types
CIt makes properties optional
DIt renames the keys using the template literal expression
✗ Incorrect
The 'as' keyword lets you rename keys in a mapped type, often using template literals to create new property names.
Which TypeScript feature allows creating new string types by combining other string types?
ATemplate literal types
BUnion types
CIntersection types
DEnums
✗ Incorrect
Template literal types let you build new string types by combining or transforming existing string types.
What does this mapped type produce?
type ReadonlyKeys<T> = { readonly [K in keyof T]: T[K] }
AA type with all properties readonly
BA type with all properties optional
CA type with keys renamed
DA type with function properties
✗ Incorrect
This mapped type makes every property in T readonly by adding the readonly modifier.
In this example, what is the type of 'getName'?
type Person = { name: string };
type Getters = { [K in keyof Person as `get${Capitalize}`]: () => Person[K] };
A() => number
B() => string
Cstring
Dnumber
✗ Incorrect
'getName' is a function returning the original 'name' property type, which is string.
Why might you use mapped types with template literals in a real project?
ATo improve runtime performance
BTo write less code for loops
CTo automatically create related property names and types, reducing manual errors
DTo create CSS styles
✗ Incorrect
Mapped types with template literals help generate consistent property names and types automatically, improving code safety and maintainability.
Explain how mapped types and template literal types can be combined to transform property names in TypeScript.
Think about how you can rename keys by adding text before or after the original key.
You got /4 concepts.
Describe a practical use case where mapped types with template literals improve your TypeScript code.
Consider how you might create functions for each property automatically.
You got /4 concepts.
Practice
(1/5)
1.
What does a mapped type with template literals in TypeScript allow you to do?
Example: { [K in keyof T as `get${Capitalize}`]: () => T[K] }
easy
A. Create new variables at runtime
B. Rename keys dynamically in object types
C. Change values of an object at runtime
D. Execute functions based on key names
Solution
Step 1: Understand mapped types
Mapped types create new object types by transforming keys or values of an existing type.
Step 2: Role of template literals
Template literals let you rename keys dynamically by combining strings and key names.
Final Answer:
Rename keys dynamically in object types -> Option B
Quick Check:
Mapped type + template literals = dynamic key renaming [OK]
A. type Prefixed = { [K in keyof Handlers as K extends `handle${string}` ? `on${Capitalize}` : never]: Handlers[K] };
B. type Prefixed = { [K in keyof Handlers as `on${Capitalize}`]: Handlers[K] };
C. type Prefixed = { [K in keyof Handlers as K extends string ? `on${Capitalize<K>}` : never]: Handlers[K] };
D. type Prefixed = { [K in keyof Handlers as K extends `on${string}` ? `handle${Capitalize}` : never]: Handlers[K] };
Solution
Step 1: Filter keys starting with 'handle'
Use conditional type K extends `handle${string}` to select only keys starting with 'handle'.
Step 2: Rename keys with 'on' prefix and capitalize
Use template literal `on${Capitalize}` to rename keys accordingly.
Step 3: Assign original value types
Keep the original function types from Handlers[K].
Final Answer:
type Prefixed = { [K in keyof Handlers as K extends `handle${string}` ? `on${Capitalize}` : never]: Handlers[K] }; -> Option A
Quick Check:
Conditional key remapping with prefix and capitalize = type Prefixed = { [K in keyof Handlers as K extends `handle${string}` ? `on${Capitalize}` : never]: Handlers[K] }; [OK]
Hint: Use conditional types to filter keys before renaming [OK]
Common Mistakes:
Not filtering keys by prefix
Applying rename to all keys
Incorrect conditional syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.