0
0
Typescriptprogramming~10 mins

Key remapping with as clause in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Key remapping with as clause
Start with object
Destructure keys
Use 'as' to rename keys
Assign new variable names
Use new variables in code
End
This flow shows how to take keys from an object and rename them using 'as' during destructuring.
Execution Sample
Typescript
const user = { name: 'Alice', age: 30 };
const { name: userName, age: userAge } = user;
console.log(userName, userAge);
This code takes keys 'name' and 'age' from 'user' and renames them to 'userName' and 'userAge'.
Execution Table
StepActionExpressionResult
1Define objectconst user = { name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }
2Destructure with renamingconst { name: userName, age: userAge } = useruserName = 'Alice', userAge = 30
3Log variablesconsole.log(userName, userAge)Output: Alice 30
4EndNo more codeExecution stops
💡 All keys destructured and renamed, program ends after logging.
Variable Tracker
VariableStartAfter Step 2Final
userundefined{ name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }
userNameundefined'Alice''Alice'
userAgeundefined3030
Key Moments - 2 Insights
Why do we write 'name: userName' instead of just 'userName'?
Because 'name' is the original key in the object, and 'userName' is the new variable name we want to use. The 'as' clause syntax 'name: userName' means 'take the value of key name and store it in variable userName' (see execution_table step 2).
What happens if we write 'const { userName } = user;' without renaming?
It looks for a key named 'userName' in the object, which does not exist, so 'userName' will be undefined. The renaming with 'as' is necessary to map existing keys to new variable names.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 2, what is the value of 'userAge'?
Aundefined
B30
C'age'
D'Alice'
💡 Hint
Check the 'Result' column in step 2 where 'userAge' is assigned.
At which step does the program output 'Alice 30'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where console.log is called in the execution_table.
If we change the code to 'const { age: userName } = user;', what will be the value of 'userName' after step 2?
A30
B'Alice'
Cundefined
DError
💡 Hint
Check how renaming works in the variable_tracker and execution_table step 2.
Concept Snapshot
Key remapping with 'as' clause:
const { originalKey: newVarName } = object;
- Extracts 'originalKey' from object
- Assigns its value to 'newVarName'
- Useful to rename variables during destructuring
- Keeps code clear and avoids naming conflicts
Full Transcript
This visual execution shows how to rename keys from an object during destructuring in TypeScript. We start with an object 'user' with keys 'name' and 'age'. Using the syntax 'const { name: userName, age: userAge } = user;', we extract the values of 'name' and 'age' and assign them to new variables 'userName' and 'userAge'. The execution table traces each step: defining the object, destructuring with renaming, logging the new variables, and ending the program. The variable tracker shows how 'userName' and 'userAge' get their values after destructuring. Key moments clarify why renaming is needed and what happens if we skip it. The quiz tests understanding of variable values at each step and effects of changing the renaming. This helps beginners see exactly how key remapping works in practice.