Type erasure means TypeScript removes all type information when it turns your code into JavaScript. This helps your code run in any browser or environment that understands JavaScript.
Type erasure and its consequences in Typescript
function identity<T>(arg: T): T { return arg; }
Type parameters like <T> exist only during development and are removed in the final JavaScript.
At runtime, TypeScript code behaves like plain JavaScript without type checks.
T is a placeholder for any type.function identity<T>(arg: T): T { return arg; }
T is a number. But at runtime, this is just identity(42).const result = identity<number>(42); console.log(result);
function logLength<T>(arg: T): T { // console.log(arg.length); // Error: length might not exist return arg; }
This program shows a generic function that returns the input. Types help during coding but disappear when running.
function identity<T>(arg: T): T { return arg; } const num = identity<number>(123); const str = identity<string>("hello"); console.log(num); console.log(str);
Type erasure means you cannot use types to check or change behavior at runtime.
Be careful with assumptions about types when your code runs, because type info is gone.
Use runtime checks (like typeof) if you need to verify types while running.
TypeScript removes all type info when creating JavaScript code.
Types help you write safer code but do not exist when the code runs.
You must use JavaScript checks if you want to test types at runtime.
Practice
type erasure mean in TypeScript?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 AQuick Check:
Type erasure = types removed at compile time [OK]
- Thinking types exist at runtime
- Believing TypeScript adds runtime type checks
- Confusing type erasure with type inference
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'stypeofoperator works at runtime to check primitive types like string or number.Final Answer:
Usingtypeofoperator in JavaScript code. -> Option AQuick Check:
Runtime type check = typeof operator [OK]
- Trying to use TypeScript types at runtime
- Using interfaces for runtime checks
- Confusing instanceof with TypeScript types
function isString(value: any): boolean {
return typeof value === 'string';
}
console.log(isString(123));
console.log(isString('hello'));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.Final Answer:
false\ntrue -> Option CQuick Check:
typeof 123 = number (false), typeof 'hello' = string (true) [OK]
- Assuming TypeScript types affect runtime
- Expecting true for number input
- Confusing output order
value is a number at runtime:function checkNumber(value: any): boolean {
if (value instanceof number) {
return true;
}
return false;
}Solution
Step 1: Understand instanceof usage
instanceofworks with objects, not primitive types like number.Step 2: Identify error cause
Usingvalue instanceof numbercauses a runtime error becausenumberis not a constructor function.Final Answer:
Usinginstanceofwith a primitive type causes a runtime error. -> Option DQuick Check:
instanceof with primitives = error [OK]
- Using instanceof with primitive types
- Thinking instanceof works like typeof
- Ignoring runtime errors from wrong instanceof usage
string | number and returns their length or value doubled?function process(input: string | number) {
// What is the best way to handle types here?
}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
Usingtypeoflets you detect if input is 'string' or 'number' and handle each case safely.Step 3: Avoid incorrect methods
Relying only on TypeScript types or usinginstanceofwith primitives is unsafe or incorrect.Final Answer:
Use JavaScript runtime checks liketypeofto distinguish types and process accordingly. -> Option BQuick Check:
Runtime type safety = typeof checks [OK]
- Assuming TypeScript types exist at runtime
- Using instanceof with primitives
- Ignoring runtime type checks
Ready to prove it?
Pass the test and this pattern moves unknown
