0
0
Typescriptprogramming~20 mins

Key remapping with as clause in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Key Remapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output 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]
};
A{ new_a: number; new_b: string; new_c: boolean }
B{ a: number; b: string; c: boolean }
C{ new_a: string; new_b: string; new_c: string }
D{ new_a: number; b: string; c: boolean }
Attempts:
2 left
💡 Hint
The as clause renames keys by prefixing them with 'new_'.
Predict Output
intermediate
2:00remaining
Resulting 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]
};
A{}
B{ name: string }
C{ id: number; name: string; active: boolean }
D{ id: number; active: boolean }
Attempts:
2 left
💡 Hint
Keys not matching the condition become never and are removed.
🔧 Debug
advanced
2:00remaining
Identify 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] };
AType 'number' cannot be used as a key type in mapped types
BNo error, type R is { number: number }
CSyntaxError: Unexpected token 'as'
DType 'string' is expected but 'number' found
Attempts:
2 left
💡 Hint
Mapped type keys must be string, number, or symbol literals or unions.
📝 Syntax
advanced
2:00remaining
Correct 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 */ };
A[K as `new_${K}` in keyof Original]: Original[K]
B[K in keyof Original as `new_${K}`]: Original[K]
C[K in keyof Original] as `new_${K}`: Original[K]
D[K in keyof Original]: Original[K] as `new_${K}`
Attempts:
2 left
💡 Hint
The as clause goes immediately after the key variable in the brackets.
🚀 Application
expert
3:00remaining
Create 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 */ };
A{ [K in keyof T as K.toUpperCase()]: T[K] }
B{ [K in keyof T as string.toUpperCase(K)]: T[K] }
C{ [K in keyof T as Uppercase<string & K>]: T[K] }
D{ [K in keyof T as Uppercase<K>]: T[K] }
Attempts:
2 left
💡 Hint
Use the built-in Uppercase utility type with a string intersection.