Understanding Type Erasure and Its Consequences in TypeScript
📖 Scenario: You are building a simple TypeScript program to understand how generic types behave at runtime. TypeScript removes type information when it compiles to JavaScript, which is called type erasure. This means types exist only during development and are not present when the code runs.
🎯 Goal: Learn how TypeScript's type erasure works by creating a generic function, adding a type variable, using the generic function, and then observing the output at runtime.
📋 What You'll Learn
Create a generic function with a type parameter
Add a variable to hold a value of the generic type
Call the generic function with a specific type
Print the result to observe type erasure effects
💡 Why This Matters
🌍 Real World
Understanding type erasure helps developers know why TypeScript types do not exist at runtime and how to write safer code that does not rely on runtime type information.
💼 Career
Many TypeScript developers need to understand type erasure to debug issues, write type-safe code, and integrate TypeScript with JavaScript libraries that do not have type information.
Progress0 / 4 steps
1
Create a generic function
Write a generic function called identity with a type parameter T that takes a parameter value of type T and returns it.
Typescript
Hint
Use after the function name to declare a generic type parameter.
2
Add a variable with a generic type
Create a variable called myNumber of type number and assign it the value 42.
Typescript
Hint
Use const myNumber: number = 42; to declare the variable.
3
Call the generic function with a specific type
Call the identity function with the type number and argument myNumber. Store the result in a variable called result.
Typescript
Hint
Use identity(myNumber) to call the function with type number.
4
Print the result to observe type erasure
Write a console.log statement to print the value of result. Notice that the output is just the value without any type information.
Typescript
Hint
Use console.log(result); to print the value.
Practice
(1/5)
1. What does type erasure mean in TypeScript?
easy
A. TypeScript removes all type information when compiling to JavaScript.
B. TypeScript keeps all type information in the final JavaScript code.
C. TypeScript converts types into runtime checks automatically.
D. TypeScript adds extra type annotations in the JavaScript output.
Solution
Step 1: Understand TypeScript compilation
TypeScript compiles to JavaScript by removing all type annotations and interfaces.
Step 2: Define type erasure meaning
This removal of type info is called type erasure, meaning types exist only during development, not at runtime.
Final Answer:
TypeScript removes all type information when compiling to JavaScript. -> Option A
Quick Check:
Type erasure = types removed at compile time [OK]
Hint: Remember: TypeScript types vanish in JavaScript output [OK]
Common Mistakes:
Thinking types exist at runtime
Believing TypeScript adds runtime type checks
Confusing type erasure with type inference
2. Which of the following is the correct way to check a variable's type at runtime in TypeScript?
easy
A. Using typeof operator in JavaScript code.
B. Using TypeScript type annotations like string in if statements.
C. Using TypeScript interfaces to check types at runtime.
D. Using instanceof with TypeScript types.
Solution
Step 1: Recall type erasure effect
TypeScript types do not exist at runtime, so you cannot use them in runtime checks.
Step 2: Identify runtime type check method
JavaScript's typeof operator works at runtime to check primitive types like string or number.
Final Answer:
Using typeof operator in JavaScript code. -> Option A
Quick Check:
Runtime type check = typeof operator [OK]
Hint: Use JavaScript's typeof for runtime type checks [OK]
Common Mistakes:
Trying to use TypeScript types at runtime
Using interfaces for runtime checks
Confusing instanceof with TypeScript types
3. What will be the output of this TypeScript code after compilation and running in JavaScript?
function isString(value: any): boolean {
return typeof value === 'string';
}
console.log(isString(123));
console.log(isString('hello'));
medium
A. true\ntrue
B. false\nfalse
C. false\ntrue
D. TypeError at runtime
Solution
Step 1: Understand the function logic
The function returns true if the input's JavaScript type is 'string', false otherwise.
Step 2: Evaluate the calls
For 123, typeof is 'number', so returns false. For 'hello', typeof is 'string', so returns true.
Hint: Check typeof results for each input value [OK]
Common Mistakes:
Assuming TypeScript types affect runtime
Expecting true for number input
Confusing output order
4. Identify the error in this TypeScript code that tries to check if value is a number at runtime:
function checkNumber(value: any): boolean {
if (value instanceof number) {
return true;
}
return false;
}
medium
A. The function should use typeof value === 'number' instead.
B. The function should use value.constructor === Number.
C. The function is correct and will return true for numbers.
D. Using instanceof with a primitive type causes a runtime error.
Solution
Step 1: Understand instanceof usage
instanceof works with objects, not primitive types like number.
Step 2: Identify error cause
Using value instanceof number causes a runtime error because number is not a constructor function.
Final Answer:
Using instanceof with a primitive type causes a runtime error. -> Option D
Quick Check:
instanceof with primitives = error [OK]
Hint: Use typeof for primitives, not instanceof [OK]
Common Mistakes:
Using instanceof with primitive types
Thinking instanceof works like typeof
Ignoring runtime errors from wrong instanceof usage
5. Given that TypeScript types are erased at runtime, how can you safely handle different input types in a function that accepts string | number and returns their length or value doubled?
function process(input: string | number) {
// What is the best way to handle types here?
}
hard
A. Rely on TypeScript types only and assume input is always correct at runtime.
B. Use JavaScript runtime checks like typeof to distinguish types and process accordingly.
C. Use instanceof to check if input is a string or number.
D. Convert input to string and always return its length.
Solution
Step 1: Recall type erasure impact
TypeScript types do not exist at runtime, so runtime checks must use JavaScript operators.
Step 2: Choose correct runtime check
Using typeof lets you detect if input is 'string' or 'number' and handle each case safely.
Step 3: Avoid incorrect methods
Relying only on TypeScript types or using instanceof with primitives is unsafe or incorrect.
Final Answer:
Use JavaScript runtime checks like typeof to distinguish types and process accordingly. -> Option B
Quick Check:
Runtime type safety = typeof checks [OK]
Hint: Always check types at runtime with typeof for unions [OK]
Common Mistakes:
Assuming TypeScript types exist at runtime
Using instanceof with primitives
Ignoring runtime type checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.