📖 Scenario: Imagine you are learning TypeScript to write safer JavaScript code. You want to understand which types help you catch mistakes before running your program but do not exist when the program runs.
🎯 Goal: You will create some TypeScript types that exist only during compile time and see how they help check your code without affecting the final JavaScript output.
📋 What You'll Learn
Create a type alias called Fruit with exact string values 'apple', 'banana', and 'orange'.
Create an interface called Person with properties name (string) and age (number).
Create a variable favoriteFruit of type Fruit and assign it the value 'banana'.
Create a variable user of type Person with name 'Alice' and age 30.
Print the values of favoriteFruit and user.name.
💡 Why This Matters
🌍 Real World
TypeScript types help developers write safer code by catching mistakes early without slowing down the running program.
💼 Career
Understanding compile-time types is essential for frontend and backend developers using TypeScript to build reliable applications.
Progress0 / 4 steps
1
Create a type alias for fruits
Create a type alias called Fruit with the exact string values 'apple', 'banana', and 'orange'.
Typescript
Hint
Use the type keyword and union of string literals.
2
Create an interface for a person
Create an interface called Person with properties name of type string and age of type number.
Typescript
Hint
Use the interface keyword and define properties with types.
3
Create variables using the types
Create a variable called favoriteFruit of type Fruit and assign it the value 'banana'. Then create a variable called user of type Person with name set to 'Alice' and age set to 30.
Typescript
Hint
Use const to declare variables with the types you created.
4
Print the variable values
Write two console.log statements to print the values of favoriteFruit and user.name.
Typescript
Hint
Use console.log(favoriteFruit) and console.log(user.name).
Practice
(1/5)
1. Which of the following TypeScript types exist only at compile time and do not produce any JavaScript code?
easy
A. Functions and variables
B. Classes and enums
C. Interfaces and type aliases
D. Objects and arrays
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 C
Quick Check:
Compile-time only types = Interfaces, type aliases [OK]
Hint: Remember: interfaces and types vanish after compiling [OK]
Common Mistakes:
Thinking classes are compile-time only
Confusing enums with type aliases
Assuming variables are compile-time types
2. Which syntax correctly declares a type alias that exists only at compile time in TypeScript?
easy
A. type User = { name: string; age: number };
B. const User = { name: 'Alice', age: 30 };
C. class User { name: string; age: number; }
D. enum User { Name, Age }
Solution
Step 1: Identify type alias syntax
Using type 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 A
Quick Check:
Type alias syntax = type ... = ... [OK]
Hint: Type aliases start with 'type' keyword [OK]
Common Mistakes:
Using const or class instead of type alias
Confusing enums with types
Missing semicolon in type alias
3. What will be the output of the following TypeScript code when compiled and run in JavaScript?
interface Person { name: string; age: number; }
const p: Person = { name: 'Bob', age: 25 };
console.log(typeof Person);
medium
A. "undefined"
B. "object"
C. "function"
D. "string"
Solution
Step 1: Understand interface removal at compile time
Interfaces do not exist in JavaScript output, so Person is not defined at runtime.
Step 2: Evaluate typeof on undefined identifier
Using typeof Person when Person is not defined returns "undefined" as a string.
Hint: Interfaces disappear; typeof interface is undefined [OK]
Common Mistakes:
Expecting 'object' because of interface
Thinking interface is a function
Confusing interface with class
4. Identify the error in this TypeScript code snippet:
type Animal = { species: string };
console.log(Animal);
medium
A. Missing semicolon after type declaration
B. Animal should be declared as an interface
C. Type alias syntax is incorrect
D. Animal is not defined at runtime, so console.log causes an error
Solution
Step 1: Recognize type alias compile-time only nature
Type aliases do not exist in JavaScript, so Animal is not a runtime value.
Step 2: Understand console.log usage
Trying to log Animal causes a runtime error because it is undefined in JavaScript.
Final Answer:
Animal is not defined at runtime, so console.log causes an error -> Option D
Quick Check:
Logging type alias = runtime error [OK]
Hint: Type aliases can't be used as runtime values [OK]
Common Mistakes:
Thinking type aliases exist at runtime
Ignoring runtime vs compile-time difference
Assuming missing semicolon causes error
5. You want to create a function that accepts only objects matching a specific shape but does not add any runtime overhead. Which TypeScript feature should you use?
hard
A. Use a type alias and create a runtime object
B. Use an interface to define the object shape
C. Use a class to define the object shape
D. Use an enum to define the object shape
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 B
Quick Check:
Interfaces = compile-time only, no runtime overhead [OK]
Hint: Interfaces check shape without runtime cost [OK]
Common Mistakes:
Using classes which add runtime code
Confusing enums with object shapes
Thinking type aliases create runtime objects
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.