TypeScript Interview Questions
TypeScript can prove things about your code before it runs, but it cannot prove that an HTTP response has the shape you expect. Use these questions to practice making that distinction and choosing types that expose mistakes early.
Try answering aloud before opening each explanation.
Try the TypeScript practice demoFoundations
When should a function accept unknown rather than any?
Use unknown when callers can supply arbitrary input but the implementation must check it before using it. For example, an event decoder must verify that the input is a non-null object and validate the relevant fields. Any permits unchecked property access and can propagate an unchecked type to callers.
What does it mean for TypeScript to use structural typing?
Compatibility generally depends on available members and their types, not on whether two declarations share a name. An object with an ID and a timestamp can be passed to a function that only requires the ID. A fresh object literal can still trigger excess-property checks, and private or protected class members impose additional constraints.
When can you use a type alias where an interface is not enough?
An alias can name a union such as Loading | Ready | Failed, a primitive, or a mapped type. Interfaces describe object shapes and can be reopened through declaration merging. For a plain object contract either can work; choose based on whether you want merging or need to name a type that is not an object shape.
With strictNullChecks enabled, what would you return from a lookup that might miss?
Include the possibility of absence in the return type, such as User | undefined. The caller must handle the missing case before dereferencing the result. A non-null assertion removes the type error, not the possibility of a miss, so reserve it for a separately established invariant.
Why can an object property become string even if you initialized it with one string literal?
A mutable property may later be assigned a different string, so inference can widen its type even when the object variable is const. A literal annotation or an as const assertion can keep the literal type. As const also marks literal properties readonly in the type system; it does not freeze the object at runtime.
Applied
How do you make adding a new result variant cause a compile error in old handlers?
Give each variant a shared discriminant with a different literal value. Switch on that discriminant and require the remaining value to be never in the fallback. If a new variant is not handled, it is still a possible value in the fallback and will not be assignable to never.
Why use a generic rather than a union for a function that picks one property from an object?
A generic can express the relationship between the key and the return type. With T for the object and K constrained to keyof T, the return type T[K] refers to exactly that key. Returning a union of all property types would make the caller narrow a result whose concrete type was already known at the call site.
When would you use satisfies instead of explicitly annotating a configuration object?
Use satisfies to check that an expression conforms to a contract without replacing its result type with the contract. For example, check a handler map against a required set of events while retaining the inferred types of its specific entries. It is a compile-time check, not a validator for configuration loaded from a file.
Why can Partial of an object type be a poor contract for an update request?
Partial makes every top-level property optional, including fields such as IDs that a caller may not be allowed to change. It also does not specify whether a missing field means no change or how to clear a value. Define the allowed fields and make omission, replacement and clearing explicit in the API contract.
When is an optional property different from a required property whose value can be undefined?
A required property must be present even if its type allows undefined. An optional property can be absent, which matters to Object.keys, object spread and the in operator. With exactOptionalPropertyTypes, setting an optional property to undefined explicitly is rejected unless undefined is part of its declared value type.
Boundary and debugging scenarios
A response is assigned to User after JSON parsing. What evidence do you need before using it?
Check the HTTP result and treat the decoded value as untrusted. Validate the fields and nested shapes that the application relies on before returning a User. An annotation or type assertion does not inspect JSON; an invalid payload otherwise reaches consumers as though it had been verified.
A lookup typed Record<string, Item> sometimes returns undefined. What should the types tell you?
A string index signature does not make every key exist at runtime. Enable noUncheckedIndexedAccess to reflect a potentially missing indexed value, or expose a lookup that explicitly returns Item | undefined. For a genuinely exhaustive map prefer a finite key union and check that all those keys are supplied.
Can a validator declared as input is Order still cause a runtime failure?
Yes. TypeScript does not prove that the declared predicate covers every required field. A check for a single ID could promise too much about nested items or status. Test invalid as well as valid inputs and derive the type from a runtime schema where that reduces duplication.
Why might an import compile but fail when a Node.js process starts?
The compiler can resolve a path using options that the runtime does not implement. For example, paths can help TypeScript find source files without rewriting those specifiers in emitted JavaScript. Match module resolution to the actual host, use import paths the host understands, and test the emitted application rather than only running the type checker.
How would you migrate a JavaScript boundary without replacing uncertainty with type assertions?
Start with one boundary that has a clear contract. Treat incoming values as unknown, validate what is needed and return a narrow typed result to consumers. Keep checks for missing or malformed data in tests. Then migrate callers incrementally so the strongest guarantees are concentrated where data enters the typed code.
Further reading
Official documentation
Related question sets
- Node.js interview questions
- REST API interview questions
- Microservices interview questions
- SQL interview questions
- Python interview questions
- JavaScript interview questions
- React interview questions
- Kubernetes interview questions
- System Design interview questions
- Java interview questions
- Docker interview questions
- MongoDB interview questions
- PostgreSQL interview questions
- Redis interview questions
- NestJS interview questions
- RabbitMQ interview questions