0
0
Typescriptprogramming~5 mins

Type erasure and its consequences in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is type erasure in TypeScript?
Type erasure means that TypeScript removes all type information when it compiles code to JavaScript. The types exist only during development and are not present at runtime.
Click to reveal answer
beginner
Why does TypeScript use type erasure?
TypeScript uses type erasure to keep the output JavaScript clean and compatible with all JavaScript environments, which do not support types natively.
Click to reveal answer
intermediate
What is a common consequence of type erasure when using generics in TypeScript?
Since types are erased, you cannot check or enforce generic types at runtime. For example, you can't check if a value matches a generic type parameter inside the compiled JavaScript.
Click to reveal answer
intermediate
How does type erasure affect debugging TypeScript code?
Because types are removed, debugging happens on plain JavaScript code. This means you can't see TypeScript types in runtime errors or logs, only JavaScript values.
Click to reveal answer
intermediate
Can you use type information at runtime in TypeScript? Why or why not?
No, you cannot use type information at runtime because TypeScript removes all types during compilation. To use type-like checks at runtime, you must write explicit JavaScript code or use libraries.
Click to reveal answer
What happens to TypeScript types after compilation?
AThey are converted to JavaScript classes
BThey are converted to runtime type checks
CThey are kept as comments in the JavaScript
DThey are removed and do not exist in the output JavaScript
Why can't you check generic types at runtime in TypeScript?
ABecause TypeScript does not allow generics
BBecause generics are only a compile-time feature and erased in JavaScript
CBecause JavaScript does not support functions
DBecause generics are converted to strings
Which of these is a consequence of type erasure?
ATypeScript code cannot be compiled
BYour code runs slower because of type checks
CYou cannot rely on types during runtime
DTypes are available as JavaScript objects
How can you perform type-like checks at runtime in TypeScript?
ABy writing explicit JavaScript checks or using libraries
BBy relying on TypeScript's runtime type system
CBy enabling a special compiler flag
DBy using TypeScript enums
What does type erasure help TypeScript achieve?
ACompatibility with JavaScript environments
BFaster runtime type checking
CAutomatic type inference at runtime
DKeeping types in the final code
Explain what type erasure is and why TypeScript uses it.
Think about what happens to types when TypeScript turns into JavaScript.
You got /3 concepts.
    Describe one consequence of type erasure when working with generics in TypeScript.
    Consider what you cannot do with generic types after compilation.
    You got /3 concepts.