MEAN TypeScript Fundamentals for Angular — Questions and Answers
Question 1: In TypeScript, what is the difference between an `interface` and a `type` alias when defining the shape of an object?
- Interfaces can be extended and merged via declaration merging; type aliases cannot be re-opened (Correct answer)
- Type aliases are compiled into JavaScript; interfaces are erased at compile time
- Interfaces only work with classes; type aliases work with any shape
- Type aliases enforce stricter null checking than interfaces
Correct answer: Interfaces can be extended and merged via declaration merging; type aliases cannot be re-opened
A key difference is declaration merging: you can declare an interface with the same name multiple times and TypeScript merges them. Type aliases are closed — redeclaring the same name is an error. Both are erased at compile time and produce no runtime code.
Question 2: What does the TypeScript `readonly` modifier do when applied to a class property?
- It prevents the property from being reassigned after the constructor runs (Correct answer)
- It makes the property private so external code cannot access it
- It causes TypeScript to serialize the property to JSON automatically
- It marks the property as optional in the constructor signature
Correct answer: It prevents the property from being reassigned after the constructor runs
readonly means the property can only be assigned during declaration or in the constructor. Any attempt to reassign it afterwards is a TypeScript compile-time error. It does not affect visibility (that is controlled by private/public/protected).
Question 3: Which TypeScript utility type creates a new type with all properties of `T` set to optional?
- Required<T>
- Partial<T> (Correct answer)
- Readonly<T>
- Pick<T, K>
Correct answer: Partial<T>
Partial<T> transforms every property of type T into an optional property (adds ?). This is commonly used for update DTOs or patch payloads where only some fields may be provided. Required<T> does the opposite, making all properties mandatory.
Question 4: In Angular, the `@Injectable({ providedIn: 'root' })` decorator uses TypeScript decorators. What does this specific metadata tell Angular?
- Create a single shared instance of this service in the root injector, available application-wide (Correct answer)
- This class can only be injected into the root component
- The service is lazily loaded and not included in the initial bundle
- The service must be manually added to a module's providers array
Correct answer: Create a single shared instance of this service in the root injector, available application-wide
providedIn: 'root' registers the service with Angular's root injector as a singleton, making it available throughout the entire application without needing to list it in any NgModule's providers array. Angular also tree-shakes it if unused.
Question 5: What does the TypeScript `!` non-null assertion operator do?
- It tells the TypeScript compiler to treat the value as non-null and non-undefined, bypassing the null check (Correct answer)
- It negates a boolean value at runtime
- It throws a runtime error if the value is null
- It converts a nullable value to a boolean false
Correct answer: It tells the TypeScript compiler to treat the value as non-null and non-undefined, bypassing the null check
The ! postfix operator is a compile-time hint to TypeScript that you are certain a value is not null or undefined. It suppresses the type error without adding any runtime check. Misusing it can lead to runtime errors if the value actually is null.
Question 6: In TypeScript, what is a generic function and why is it useful in MEAN stack services?
- A function parameterized by a type variable, allowing it to work with multiple types while preserving type safety (Correct answer)
- A function that accepts any number of arguments using the spread operator
- A function declared without a return type, defaulting to `any`
- A static method shared across all class instances
Correct answer: A function parameterized by a type variable, allowing it to work with multiple types while preserving type safety
Generics let you write functions like `getData<T>(url: string): Observable<T>` where T is inferred from usage. This means the Angular HTTP service can return Observable<User[]> or Observable<Product> with full type checking, rather than returning untyped `any` responses.
In TypeScript, what is the difference between an `interface` and a `type` alias when defining the shape of an object?