Fundamental Type System (Questions 1-10)
-
Explain the difference between
typeandinterfacein TypeScript. When would you use one over the other? This tests understanding of declaration merging, computed properties, and the practical implications of each approach. -
What is structural typing in TypeScript, and how does it differ from nominal typing? Understanding this concept is crucial for grasping why TypeScript allows certain assignments that might seem surprising.
-
Explain the
unknowntype and how it differs fromany. Why wasunknownintroduced? This reveals whether a candidate understands type safety and the progression of TypeScript’s type system. -
What is type narrowing, and what are the different ways to narrow types in TypeScript? This covers type guards, discriminated unions, and control flow analysis.
-
Explain the difference between
neverandvoid. Provide use cases for thenevertype. Understanding these utility types shows comprehension of TypeScript’s type algebra. -
What are union types and intersection types? Explain with examples where each would be most appropriate. This tests fundamental understanding of type composition.
-
Describe the difference between
enumandconst enum. What are the runtime implications of each? This reveals understanding of how TypeScript compiles to JavaScript. -
What is type assertion in TypeScript? What’s the difference between
asand angle bracket<>syntax, and when should you avoid type assertions? This tests whether candidates understand the dangers of type assertions. -
Explain what index signatures are and their limitations. How do they differ from mapped types? This reveals understanding of dynamic property access and type system constraints.
-
What is the purpose of the
readonlymodifier? How does it differ fromconst? This tests understanding of immutability at the type level versus runtime.
Advanced Types and Generics (Questions 11-20)
-
Explain what generic constraints are and provide a real-world example where they solve a specific problem. This tests practical application of generics beyond simple container types.
-
What are conditional types in TypeScript? Explain the
T extends U ? X : Ysyntax and provide an example. Understanding conditional types is essential for advanced TypeScript usage. -
Explain the
inferkeyword in conditional types. How would you use it to extract the return type of a function? This is a common advanced interview question that tests deep type system knowledge. -
What are template literal types, and how can they be used to create more precise types? This tests knowledge of TypeScript 4.1+ features and string manipulation at the type level.
-
Explain what mapped types are and how they work. Implement a simple version of the
Partialutility type. This reveals understanding of type transformations. -
What is the difference between covariance, contravariance, and invariance in TypeScript? How does this apply to function parameters? This is an advanced question that separates senior developers from others.
-
Explain what distributive conditional types are. Why does
T extends U ? X : Ydistribute over union types? This tests deep understanding of how TypeScript’s type system processes unions. -
How do you create a type-safe event emitter using generics? This is a practical question that tests ability to apply generics to real-world patterns.
-
What are higher-order types, and how would you implement a
Currytype that represents a curried function? This tests advanced generic manipulation skills. -
Explain what recursive types are and provide an example of when you’d use them, such as representing a nested tree structure. Understanding recursive types is crucial for complex data structures.
Utility Types and Type Manipulation (Questions 21-30)
-
Implement the
PickandOmitutility types from scratch. Explain how they work internally. This tests understanding of mapped types and keyof operators. -
What is the
keyofoperator, and how does it relate totypeof? Provide examples of using them together. This reveals understanding of type queries and index access. -
Explain the
ReturnTypeandParametersutility types. How would you implement them? This tests knowledge of conditional types and the infer keyword. -
What is the difference between
Record<K, V>and an index signature? When would you prefer one over the other? This tests practical understanding of object type construction. -
Implement a
DeepReadonlytype that makes all properties readonly recursively, including nested objects and arrays. This is a challenging question that tests advanced mapped type and conditional type skills. -
How would you create a type that represents all possible paths through an object as string literal types? This tests ability to work with template literal types and recursive types together.
-
Explain the
Exclude,Extract, andNonNullableutility types. Implement one from scratch. This tests understanding of conditional types and type filtering. -
What is the
Awaitedutility type, and why is it useful for working with promises? This tests understanding of modern TypeScript features for async operations. -
How would you create a type that makes specific properties required while keeping others optional? This tests ability to combine utility types creatively.
-
Implement a
UnionToIntersectiontype. Explain how it works using contravariance. This is a very advanced question that tests deep type system knowledge.
Practical Application and Patterns (Questions 31-40)
-
How do you implement a builder pattern in TypeScript that ensures required properties are set before building? This tests ability to use the type system for API design.
-
Explain how to use TypeScript with React: What are the differences between
FC,ReactElement,ReactNode, andJSX.Element? Since you work with React, this is directly relevant. -
How do you type a higher-order component (HOC) in TypeScript? This tests understanding of generics and React patterns together.
-
What are discriminated unions, and how do they enable exhaustive type checking with switch statements? This is a crucial pattern for type-safe state machines.
-
How would you implement type-safe Redux actions and reducers? This tests practical application of union types and type narrowing.
-
Explain how to create a type-safe API client where endpoints are strongly typed based on a schema. This tests ability to apply TypeScript to real-world architectural challenges.
-
How do you handle branded types (nominal typing) in TypeScript’s structural type system? This reveals advanced techniques for creating distinct types.
-
What strategies can you use to type third-party libraries that don’t have type definitions? This tests practical experience with the TypeScript ecosystem.
-
How do you implement dependency injection in TypeScript while maintaining type safety? This tests understanding of advanced patterns and generics.
-
Explain how to use TypeScript’s type system to prevent invalid state representations (making impossible states impossible). This is a key principle of type-safe design.
Configuration, Tooling, and Best Practices (Questions 41-50)
-
Explain the most important
tsconfig.jsonoptions:strict,strictNullChecks,noImplicitAny,strictFunctionTypes. What does each enforce? This tests understanding of type safety configurations. -
What is the difference between
moduleandtargetin tsconfig? How do they affect compilation? This reveals understanding of TypeScript compilation and JavaScript ecosystem. -
Explain the
pathsandbaseUrloptions in tsconfig. How do they help with module resolution? This tests practical project configuration knowledge. -
What are declaration files (
.d.ts), and when would you create them manually? This tests understanding of type declarations and the TypeScript compilation model. -
Explain the
declarekeyword and its use cases, including ambient declarations and global augmentation. This tests knowledge of advanced declaration patterns. -
How does TypeScript handle module augmentation and declaration merging? Provide examples. This is important for extending third-party types.
-
What are the performance implications of using complex types? How can you optimize TypeScript compilation speed? This tests experience with large codebases and practical challenges.
-
Explain the
satisfiesoperator introduced in TypeScript 4.9. How does it differ from type assertions? This tests knowledge of recent TypeScript features. -
What are some common TypeScript anti-patterns, and how would you refactor them? This reveals practical experience and understanding of best practices.
-
How do you approach migrating a large JavaScript codebase to TypeScript? What strategies would you use to minimize disruption? This tests real-world experience and architectural thinking.
These questions cover the full spectrum from fundamentals to expert-level TypeScript knowledge, touching on theoretical understanding, practical application, and real-world scenarios you’d encounter in production codebases.