What if your code could catch mistakes before it even runs, saving you hours of debugging?
Why What types exist only at compile time in Typescript? - Purpose & Use Cases
Imagine you are writing a program and want to check if your data fits certain rules before running the code. Without special tools, you have to write extra checks everywhere, making your code long and confusing.
Manually checking data types during runtime slows down your program and can miss errors if you forget a check. It's like proofreading a long letter by hand every time you send it--slow and easy to miss mistakes.
TypeScript's compile-time types let you catch errors before running the program. These types exist only while you write and build your code, helping you find mistakes early without adding extra checks in the running program.
if (typeof age !== 'number') { throw new Error('Age must be a number'); }
function greet(age: number) { console.log(`You are ${age} years old`); }This lets you write safer code that catches mistakes early, making your programs more reliable and easier to maintain.
When building a website form, TypeScript ensures the data you get matches what you expect before the site runs, preventing bugs that confuse users.
Compile-time types exist only while writing and building code, not when running it.
They help catch errors early without slowing down the program.
This makes your code safer and easier to work with.
Practice
Solution
Step 1: Understand TypeScript compile-time types
Interfaces and type aliases are used only to check types during compilation and do not exist in the final JavaScript.Step 2: Identify runtime constructs
Classes, enums, functions, variables, objects, and arrays exist in the JavaScript output and run at runtime.Final Answer:
Interfaces and type aliases -> Option CQuick Check:
Compile-time only types = Interfaces, type aliases [OK]
- Thinking classes are compile-time only
- Confusing enums with type aliases
- Assuming variables are compile-time types
Solution
Step 1: Identify type alias syntax
Usingtype User = { ... }declares a type alias that exists only during compilation.Step 2: Differentiate from runtime declarations
Const objects, classes, and enums produce JavaScript code and exist at runtime.Final Answer:
type User = { name: string; age: number }; -> Option AQuick Check:
Type alias syntax = type ... = ... [OK]
- Using const or class instead of type alias
- Confusing enums with types
- Missing semicolon in type alias
interface Person { name: string; age: number; }
const p: Person = { name: 'Bob', age: 25 };
console.log(typeof Person);Solution
Step 1: Understand interface removal at compile time
Interfaces do not exist in JavaScript output, soPersonis not defined at runtime.Step 2: Evaluate typeof on undefined identifier
Usingtypeof Personwhen Person is not defined returns "undefined" as a string.Final Answer:
"undefined" -> Option AQuick Check:
Interfaces vanish, typeof interface = "undefined" [OK]
- Expecting 'object' because of interface
- Thinking interface is a function
- Confusing interface with class
type Animal = { species: string };
console.log(Animal);Solution
Step 1: Recognize type alias compile-time only nature
Type aliases do not exist in JavaScript, soAnimalis not a runtime value.Step 2: Understand console.log usage
Trying to logAnimalcauses a runtime error because it is undefined in JavaScript.Final Answer:
Animal is not defined at runtime, so console.log causes an error -> Option DQuick Check:
Logging type alias = runtime error [OK]
- Thinking type aliases exist at runtime
- Ignoring runtime vs compile-time difference
- Assuming missing semicolon causes error
Solution
Step 1: Understand compile-time type checking
Interfaces define object shapes only at compile time and do not generate JavaScript code, so no runtime overhead.Step 2: Compare with other options
Classes and enums generate runtime code; type aliases do not create runtime objects unless explicitly instantiated.Final Answer:
Use an interface to define the object shape -> Option BQuick Check:
Interfaces = compile-time only, no runtime overhead [OK]
- Using classes which add runtime code
- Confusing enums with object shapes
- Thinking type aliases create runtime objects
Ready to prove it?
Pass the test and this pattern moves unknown
