0
0
Typescriptprogramming~20 mins

Type erasure and its consequences in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Erasure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of generic function with type erasure
What is the output of this TypeScript code after compilation and running in JavaScript?
Typescript
function identity<T>(arg: T): T {
  return arg;
}

const result = identity<number>(42);
console.log(typeof result);
A"T"
B"generic"
C"number"
D"undefined"
Attempts:
2 left
💡 Hint
Remember that TypeScript types are removed during compilation.
🧠 Conceptual
intermediate
2:00remaining
Effect of type erasure on runtime type checks
Which statement best describes the effect of TypeScript's type erasure on runtime type checks?
ATypeScript types are erased and cannot be used for runtime type checks.
BTypeScript converts types into JavaScript classes for runtime use.
CTypeScript automatically inserts runtime type checks based on types.
DTypeScript types are available at runtime for type checking.
Attempts:
2 left
💡 Hint
Think about what happens to types after compilation.
🔧 Debug
advanced
3:00remaining
Why does this type guard fail at runtime?
Consider this TypeScript code snippet. Why does the type guard fail to distinguish types at runtime?
Typescript
interface Cat {
  meow(): void;
}
interface Dog {
  bark(): void;
}

function isCat(animal: Cat | Dog): animal is Cat {
  return (animal as Cat).meow !== undefined;
}

const pet = { bark: () => console.log('woof') };

if (isCat(pet)) {
  pet.meow();
} else {
  pet.bark();
}
ABecause TypeScript types are erased, the property check is unreliable at runtime.
BBecause the pet object does not have a meow property, the check always returns true.
CBecause the type guard syntax is incorrect and causes a compile error.
DBecause the pet object is missing the bark method, causing a runtime error.
Attempts:
2 left
💡 Hint
Check what happens to interfaces and type assertions at runtime.
📝 Syntax
advanced
2:00remaining
Identify the runtime error caused by type erasure
What runtime error will this TypeScript code produce after compilation and running in JavaScript?
Typescript
function getLength<T>(arg: T): number {
  return arg.length;
}

console.log(getLength<string>("hello"));
AReferenceError: arg is not defined
BTypeError: Cannot read property 'length' of undefined
CSyntaxError: Unexpected token
DNo error, outputs 5
Attempts:
2 left
💡 Hint
Consider what happens to the generic type and the argument passed.
🚀 Application
expert
3:00remaining
How to preserve type information at runtime despite type erasure?
Which approach allows you to preserve and use type information at runtime in TypeScript despite type erasure?
ARely on TypeScript's automatic runtime type preservation.
BUse decorators or explicit metadata reflection to store type info at runtime.
CUse only interfaces since they exist at runtime.
DAvoid generics to keep types at runtime.
Attempts:
2 left
💡 Hint
Think about ways to add metadata manually or with tools.