{jsonbody.com}

Top 50 TypeScript Interview Questions & Answers

Master 50 TypeScript interview questions on unknown vs any, generics, type narrowing, utility types, mapped and conditional types, and tsconfig.

TypeScript Basics

Q1. What is TypeScript?

TypeScript is a superset of JavaScript that adds static types. The compiler checks the code and emits JavaScript, and most type information is removed at runtime.

Q2. Does TypeScript check types at runtime?

No. Types are erased during compilation, so data from an API still needs runtime validation, for example with Zod or manual checks.

Q3. What is the difference between any and unknown?

any turns off type checking for a value. unknown accepts any value but must be narrowed before use, so it is the safer choice for uncertain data.

Q4. What is the never type?

never represents a value that cannot occur, such as the return of a function that always throws. It is also the result when all union cases have been narrowed away.

Q5. What is the difference between void and undefined?

void says a function's return value should not be used. undefined is a real value and type, and a function declared to return it must return undefined.

Q6. What is the difference between interface and type?

Both describe object shapes. Interfaces can be extended and merged across declarations, while type aliases can also represent unions, tuples and mapped or conditional types.

Q7. What is structural typing?

TypeScript compares types by shape rather than by name. Any value with the required properties is compatible, even if it was not declared with that type.

Q8. What is type inference?

The compiler works out types from values and usage, so const n = 5 needs no annotation. Annotations are most useful on public function signatures and when inference is too wide.

Q9. What are excess property checks?

An object literal assigned directly to a type is rejected if it has unknown properties, which catches typos. The check does not apply if the object first goes through a variable.

Q10. What are tuples?

Tuples are fixed-length arrays with a known type at each position, such as [string, number]. They suit values like React's useState return, and can be optional, named or readonly.

Unions & Narrowing

Q11. What is the difference between a union and an intersection?

A union, A | B, is a value of either type. An intersection, A & B, must satisfy both types at once.

Q12. What is type narrowing?

It refines a broad type to a more specific one using checks such as typeof, instanceof, in, equality or truthiness. The compiler follows these through the code flow.

Q13. What is a user-defined type guard?

A function that returns a type predicate, such as function isUser(x: unknown): x is User. When it returns true, the compiler treats the value as that type.

Q14. What is a discriminated union?

A union whose members share a literal field, such as type: "success" | "error". Checking that field narrows to the exact member, which makes it great for state and API results.

Q15. How do you make a switch exhaustive?

Assign the value to a never variable in the default case. If a new union member is added and not handled, the assignment becomes a compile error.

Q16. What do ?., ?? and ! do?

?. safely accesses a value that may be null or undefined, and ?? provides a fallback for those two values. ! tells the compiler a value is not null and adds no runtime check.

Q17. What is a type assertion?

value as Type tells the compiler to treat a value as a type, without any runtime check or conversion. Use narrowing or validation where possible, as a wrong assertion hides bugs.

Q18. What does as const do?

It keeps the narrowest literal types and makes properties readonly. For example, ["a", "b"] as const becomes readonly ["a", "b"], not string[].

Q19. What does the satisfies operator do?

satisfies checks that a value matches a type while keeping its more specific inferred type. An annotation, in contrast, widens the value to the declared type.

Q20. What are literal types?

Types that allow exact values, such as "GET" | "POST" or 200 | 404. Combined with unions, they often replace enums with less generated code.

Generics & Advanced Types

Q21. What are generics?

Generics are type parameters that keep the relationship between inputs and outputs. For example, function first<T>(items: T[]): T | undefined keeps the element type.

Q22. What is a generic constraint?

extends limits what a type parameter accepts. For example, <T extends { id: string }> guarantees that T has an id, so the function can safely use it.

Q23. What does keyof do?

keyof T produces a union of the property names of T. Together with generics, it enables safe property access such as function get<T, K extends keyof T>(o: T, k: K): T[K].

Q24. What does typeof do in a type position?

It takes the type of an existing value, such as type Config = typeof config. This keeps a type in sync with a value without writing it twice.

Q25. What are indexed access types?

T["name"] looks up the type of a property, and T[number] gets the element type of an array. They are useful for reusing part of an existing type.

Q26. What are mapped types?

Mapped types build a new type by looping over keys, as in { [K in keyof T]: T[K] | null }. Modifiers can add or remove readonly and ?, and as can rename keys.

Q27. What are conditional types?

Conditional types choose a type based on another, as in T extends string ? A : B. They distribute over each member when T is a bare union type parameter.

Q28. What does infer do?

Inside a conditional type, infer captures part of a type, as in T extends Promise<infer U> ? U : T. This is how utility types such as ReturnType are built.

Q29. What are template literal types?

They create string types from other types, such as on${Capitalize<Name>}. They suit patterns like event names, CSS values and route strings.

Q30. What are branded types?

A brand adds a marker, as in string & { __brand: "UserId" }, so that values with the same runtime type cannot be mixed. It is used for IDs, as TypeScript is otherwise structural.

Utility Types, Classes & Functions

Q31. What are the common utility types?

Partial, Required, Readonly, Pick, Omit, Record, Exclude, Extract, NonNullable, ReturnType, Parameters and Awaited cover most everyday type transformations.

Q32. What is the difference between Pick and Omit?

Pick<T, K> keeps only the listed properties, and Omit<T, K> keeps all except those listed. Both create a new object type.

Q33. What is the difference between Exclude and Omit?

Exclude removes members from a union type. Omit removes properties from an object type.

Q34. What does Record<K, V> do?

It creates an object type with keys K and values V. With a union of literal keys, TypeScript requires every key to be present, which helps with exhaustive lookup tables.

Q35. What access modifiers does TypeScript have?

public, protected and private, which are compile-time checks only. JavaScript #private fields are enforced at runtime as well.

Q36. What is the difference between an abstract class and an interface?

An abstract class can contain implementation and exists at runtime. An interface only describes a shape and disappears on compilation, and a class can implement several interfaces.

Q37. What are function overloads?

Several call signatures followed by one implementation, allowing the return type to depend on the arguments. Often a union or generic is simpler, so use overloads when they really add clarity.

Q38. What is the this parameter?

A fake first parameter, as in function f(this: Window), that declares what this should be. The compiler checks it and removes it from the output.

Q39. What are enums, and what are their drawbacks?

Enums define named constants and normally generate JavaScript. Many teams prefer union literals or as const objects, because they generate less code and behave more predictably.

Q40. What are decorators?

Decorators are functions applied with @ that add behaviour to classes and members. TypeScript 5 supports the standard version, while frameworks such as older Angular setups use experimentalDecorators.

Configuration & Tooling

Q41. What does strict do in tsconfig.json?

It turns on a family of stronger checks, including strictNullChecks and noImplicitAny. New projects should enable it from the start.

Q42. What does strictNullChecks do?

It treats null and undefined as separate types, so they must be handled explicitly. This catches a very common class of runtime errors at compile time.

Q43. What does noUncheckedIndexedAccess do?

It adds undefined to values read by index, such as arr[0], which better reflects that an element may be missing. It is not included in strict.

Q44. What is a declaration file?

A .d.ts file describes the types of JavaScript code without any implementation. Packages can include them, or provide them through @types/* packages.

Q45. What is declaration merging?

Multiple declarations with the same name combine into one, as with interfaces. It is often used to extend third-party types, such as adding a property to Express's Request.

Q46. What does import type do?

It imports only types and is always removed from the output, which avoids unneeded runtime imports. The verbatimModuleSyntax option makes this behaviour explicit.

Q47. What is the difference between target, module and moduleResolution?

target sets the JavaScript version emitted, module sets the module format, and moduleResolution controls how imports are found. Modern Node projects often use NodeNext.

Q48. How do Babel, esbuild and tsc differ for TypeScript?

Babel and esbuild remove types quickly without checking them, while tsc performs full type checking. A common setup uses a fast tool for builds and tsc --noEmit in CI.

Q49. How do you migrate a JavaScript project to TypeScript?

Enable allowJs, add checkJs where useful, convert files gradually, and start with shared modules. Tighten settings in steps and avoid hiding problems with any.

Q50. How do you type API responses safely?

A declared response type is only a promise, not a check. Validate at the boundary with a schema library such as Zod, then infer the static type from that schema.