What is the difference between ‘type’ and ‘interface’ in TypeScript?
Both are used to define shapes of objects, but ‘interface’ is extendable and better for OOP patterns, while ‘type’ is more versatile for unions and intersections.
How does TypeScript improve code safety in frontend/backend projects?
By providing static typing, preventing runtime errors, and offering better IDE support with autocompletion and refactoring.
What are TypeScript’s ‘utility types’ and give an example?
Utility types like Partial<T>, Pick<T, K>, Omit<T, K> help transform existing types. E.g., Partial<T> makes all properties optional.</T></T>
Explain ‘type inference’ in TypeScript.
TypeScript automatically deduces types from assigned values, reducing need for explicit type annotations.
When should you use ‘readonly’ properties?
‘readonly’ is used when a property should be assigned once and never mutated, enforcing immutability.
What is the difference between ‘public’, ‘protected’, and ‘private’ in TypeScript classes?
‘public’ is accessible everywhere, ‘protected’ is accessible in class and subclasses, ‘private’ is only within the class.
What are ‘Type Guards’ in TypeScript?
Runtime checks (like ‘typeof’, ‘instanceof’, or custom predicates) that narrow down types within a block.
What are ‘Generics’ in TypeScript and why are they useful?
Generics allow functions, classes, and interfaces to work with multiple types without losing type safety.
What’s the difference between ‘any’, ‘unknown’, and ‘never’?
‘any’ disables type checking, ‘unknown’ is safer and requires type guards, ‘never’ is for unreachable code or exhaustive checks.
How does ‘as const’ affect a variable?
It narrows down the type to a literal and marks properties as readonly, useful for immutable values.
How would you implement Dependency Inversion Principle using TypeScript interfaces?
Define interfaces for service abstractions and inject concrete implementations where needed, decoupling high-level modules from low-level details.
Explain how to use ‘Mapped Types’ in TypeScript.
Mapped types use syntax like { [K in keyof T]: T[K] } to iterate and transform properties of another type.
How can you enforce immutability deeply in an object structure?
By using Recursive Utility Types or libraries like ‘deep-freeze’, since ‘readonly’ only affects top-level properties.
How would you create a ‘type-safe API response’ type in TypeScript?
Use generics like ApiResponse<T> { data: T; error?: string; } to type API responses explicitly.</T>
What is ‘discriminated union’ and how does it help?
Unions of objects with a common literal property (discriminator) that simplifies type narrowing and exhaustive checks.
How do you create a ‘conditional type’ and give an example?
Use ternary operator Syntax like T extends U ? X : Y allows type logic. Example: type IsString<T> = T extends string ? true : false;</T>
Explain the difference between ‘interface merging’ and ‘type alias’.
Interfaces can be merged across declarations, type aliases can’t. Useful for extending modules or libraries.
How would you implement a type-safe builder pattern in TypeScript?
Use chained method returns with narrowed types at each step, leveraging generics for state enforcement.
What is ‘keyof typeof’ pattern used for?
‘keyof typeof’ is used to create union of keys from an object, ensuring type-safe access or mapping.
How would you explain the benefits of TypeScript to a team hesitant about adopting it?
TypeScript reduces runtime bugs, improves developer productivity with autocompletion and refactors, and scales better for large codebases.