TypeScript Interview Questions & Answers
Complete TypeScript interview preparation guide.
TypeScript Interview Roadmap
Junior
- Types
- Interfaces
- Enums
- Functions
- Type Aliases
Mid-Level
- Generics
- Utility Types
- Type Guards
- Union Types
- Intersection Types
Senior
- Mapped Types
- Conditional Types
- Decorators
- Advanced Generics
- Architecture
What is TypeScript and why was it created?
TypeScript is an open-source programming language developed by Microsoft. It is a superset of JavaScript that adds static typing and compile-time error checking.
Benefits
- Type Safety
- Better IntelliSense
- Improved Refactoring
- Easier Maintenance
- Better Developer Experience
Expected Answer: No. TypeScript must be transpiled into JavaScript.
What is the difference between any, unknown and never?
let value: any = 10;
let userInput: unknown = 'John';
function throwError(): never {
throw new Error('Something went wrong');
}| Type | Description |
|---|---|
| any | Disables type checking completely |
| unknown | Safer alternative to any |
| never | Function never returns |
Interface vs Type Alias
interface User {
id: number;
name: string;
}
type Employee = {
id: number;
name: string;
};Differences
- Interfaces support declaration merging
- Types can represent unions
- Types can represent primitives
- Interfaces are preferred for object contracts
What are Generics?
Generics allow writing reusable and type-safe code.
function identity<T>(value: T): T {
return value;
}
identity<string>('Hello');
identity<number>(100);Advantages
- Reusable Code
- Compile-time Type Safety
- Better IDE Support
Predict the Output
type User = {
name: string;
};
const user = {} as User;
console.log(user.name);Answer
The code compiles successfully but prints undefined.
What are Utility Types?
interface User {
id: number;
name: string;
}
type PartialUser = Partial<User>;
type ReadonlyUser = Readonly<User>;
type UserName = Pick<User, 'name'>;Common Utility Types
- Partial
- Readonly
- Pick
- Omit
- Record
- Required
Explain Type Guards
function print(value: string | number) {
if (typeof value === 'string') {
console.log(value.toUpperCase());
}
}Type Guards help TypeScript narrow types safely during runtime checks.
Common Guards
- typeof
- instanceof
- in operator
- Custom Guards
What are Conditional Types?
type IsString<T> =
T extends string
? true
: false;
type A = IsString<string>;
type B = IsString<number>;Conditional Types allow creating dynamic types based on conditions.
What are Mapped Types?
type ReadonlyType<T> = {
readonly [K in keyof T]: T[K];
};Mapped Types transform existing types into new types.
Real-world Usage
- DTO Generation
- Readonly Objects
- API Response Models
- Form Models
Build a Generic API Response Type
interface ApiResponse<T> {
success: boolean;
data: T;
message?: string;
}
const response: ApiResponse<string> = {
success: true,
data: 'User Created'
};Interview Follow-up
- How would you handle API errors?
- How would you support pagination?
- How would you model nested responses?
Why is TypeScript important for large-scale Angular applications?
Benefits
- Compile-time validation
- Improved maintainability
- Safe refactoring
- Reduced production bugs
- Better team collaboration
Strong Answer: Explains scalability, maintainability, IDE support, architecture benefits and developer productivity.
Design a Type-Safe Event System
interface Events {
login: {
userId: number;
};
logout: {
userId: number;
};
}
function emit<K extends keyof Events>(
event: K,
payload: Events[K]
) {
console.log(event, payload);
}What Interviewers Evaluate
- Generics Knowledge
- Type Inference Understanding
- Scalable Design Thinking
- Architecture Skills
Frequently Asked Questions
What are the most important TypeScript interview topics?
Types, Interfaces, Generics, Utility Types, Type Guards, Mapped Types and Advanced Type System concepts.
Are practical coding questions included?
Yes. Real-world TypeScript coding exercises and debugging questions are included.
Is this guide suitable for senior developers?
Yes. Advanced TypeScript concepts and architecture discussions are covered.
