Challenge - 5 Problems
Key Remapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of key remapping with as clause in mapped types
What is the type of
Result after applying the key remapping with as clause in this code?Typescript
type Original = { a: number; b: string; c: boolean };
type Result = {
[K in keyof Original as `new_${string & K}`]: Original[K]
};Attempts:
2 left
💡 Hint
The
as clause renames keys by prefixing them with 'new_'.✗ Incorrect
The mapped type remaps each key
K to a new key `new_${K}` while keeping the original value types.❓ Predict Output
intermediateResulting keys after conditional key remapping
Given this mapped type with conditional key remapping, what keys does
Filtered have?Typescript
type Data = { id: number; name: string; active: boolean };
type Filtered = {
[K in keyof Data as K extends 'id' | 'active' ? K : never]: Data[K]
};Attempts:
2 left
💡 Hint
Keys not matching the condition become
never and are removed.✗ Incorrect
Keys
id and active remain, name is removed by remapping to never.🔧 Debug
advancedIdentify the error in key remapping with invalid key type
What error does this TypeScript code produce?
Typescript
type T = { a: number };
type R = { [K in keyof T as number]: T[K] };Attempts:
2 left
💡 Hint
Mapped type keys must be string, number, or symbol literals or unions.
✗ Incorrect
Using a plain 'number' type as a key remapping is invalid because keys must be literal types or unions of literals.
📝 Syntax
advancedCorrect syntax for key remapping with as clause
Which option shows the correct syntax for remapping keys in a mapped type using the
as clause?Typescript
type Original = { x: number; y: string };
type Mapped = { /* fill here */ };Attempts:
2 left
💡 Hint
The
as clause goes immediately after the key variable in the brackets.✗ Incorrect
The correct syntax is
[K in keyof T as NewKey]: Value where as remaps the key.🚀 Application
expertCreate a mapped type that renames keys to uppercase
Which option correctly defines a mapped type
UppercaseKeys that takes an object type T and produces a new type with all keys converted to uppercase strings?Typescript
type UppercaseKeys<T> = { /* fill here */ };Attempts:
2 left
💡 Hint
Use the built-in
Uppercase utility type with a string intersection.✗ Incorrect
Keys must be cast to
string & K before applying Uppercase to ensure correct type inference.