TypeScript Generics Interview Questions: Practice Quiz With Answers
Reviewed by Mark Dickie · Last updated
4 questions on this page you can answer and see marked on the spot. Go to the first one
TypeScript generics are type parameters that let one function, class, or type work across many types while the compiler still checks each use. Interviewers mostly test whether you can constrain a parameter, let inference fill in type arguments, and reach for keyof and indexed access without falling back to any. If you can explain why T extends { id: string } makes item.id safe, and what breaks once that constraint is removed, you will handle most of the follow-up questions.
What does a TypeScript generics interview test?
| Concept | What to know | Typical failure |
|---|---|---|
Constraints with extends | Limits T so property access type-checks | Reading .length on an unconstrained T |
| Inference | TypeScript infers T from arguments; an explicit <T> overrides it | An array of literals widens to string[] when you wanted "a" | "b" |
keyof and T[K] | Types a lookup from a key to its value | Passing a key that is not in keyof T and getting any back |
| Conditional types | T extends U ? X : Y picks a branch at compile time | Distributing over a union when you expected one result |
infer | Pulls a piece out of a shape, such as a return type | Using infer outside a conditional type |
| Default type parameters | <T = string> is the fallback when inference has no candidate | Assuming the default applies even when inference succeeds |
Hands-on exercises worth doing before the quiz:
- Write
pick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>and confirm that a misspelled key fails to compile. - Build an
Unwrap<T>type withinferthat returns the element type of an array, and check what it returns for a non-array. - Replace an
any-typedmaphelper with a generic<T, U>signature, then verify the callback's return type flows into the result. - Write a
DeepPartial<T>mapped type and explain where it breaks on functions and arrays.
Key facts
- Tarmac has 16 TypeScript interview questions on this topic, 10 of them on this page, at difficulty 1–5 of 5.
- Tarmac tracked 1,859 job postings asking for TypeScript in September 2026.
- Roles asking for TypeScript advertise a median base salary of US$180,625, across 570 job postings as of September 2026.
- Tarmac last reviewed these TypeScript interview questions on 26 October 2026.
At a glance
| Questions | 10 shown · 16 in the bank |
|---|---|
| Difficulty | 1–5 of 5 |
| Formats | Multiple choice, Ordering, Code output, Coding exercise, Multiple answer, Find the bug, Short answer, Fill in the blank, True / false, Flashcard |
| Interactive | 1 run your code against tests, in the app |
What you'll review
- default type params
- generic constraints
- generic functions
Practice questions
Try one before you open the answer. Pick an option and press Check; it's marked on the spot.
In TypeScript, given type Box<T = string> = { value: T };, what is the type of value in const b: Box = { value: 'hi' };?#
Options
Show answer
In TypeScript, a generic type parameter's default (such as T = string) is used when no type argument is supplied. So Box written without arguments means Box<string>, and its value property is typed string. Defaults fill in omitted arguments; they do not turn the type into any or unknown.
When a generic type is used without type arguments, its default type parameter is applied. Here Box with no argument means Box<string>, so T is string and value is typed string. The literal 'hi' is assignable to that type. The default does not produce any or unknown, and number would require a different default or an explicit argument.
You start with the unconstrained generic function below, which reads .length from its arguments.#
Put these in order
Show answer
To fix a generic function that reads .length from an unconstrained type parameter, first compile the unconstrained version to see the error, then add a constraint such as T extends { length: number }. Recompile to confirm the body type-checks, and finally verify the inferred type at call sites.
The error only appears once the unconstrained body is compiled, so that step comes first. The fix is to add the constraint that tells TypeScript T has a numeric length, which must happen before the body can be re-checked. Once the body compiles, the call site can be verified to infer the expected concrete type. Each step depends on the previous one, so this is a single defensible sequence.
What does this log?#
function first<T>(arr: T[]): T | undefined {
return arr[0];
}
console.log(first([10, 20, 30]));Show answer
10
The generic first infers T = number from the argument and returns arr[0], which is 10. Types are erased at runtime; the behaviour is just plain array indexing.
Implement pluck(items, key): given an array of objects and a key, return the array of values at that key. Type it generically so key is constrained to keyof T and the return type is T[K][].#
Starter code
function pluck(items: unknown[], key: string): unknown[] {
// TODO: generic signature + implementation
return [];
}Your solution must pass
- plucks names
This one is written and run, not read. Solve it in the app and your code is executed against these tests and the hidden ones.
Consider these TypeScript declarations and calls. Select ALL statements that are true.#
Options
Pick every one that applies.
Show answer
In TypeScript, a generic interface with a default type parameter lets a bare reference like Box mean Box<string>. A default must also satisfy its constraint, so T extends string = number fails to compile. Required type parameters cannot follow defaulted ones, and inference from arguments takes precedence over the default.
A default type parameter is used when no type argument is written, so Box with no arguments resolves to Box<string>. A default must also satisfy the declared constraint, so T extends string = number is rejected with a type error. The statement about a required parameter after a defaulted one is false: TypeScript requires that required type parameters precede optional ones, so type Pair<A = number, B> fails to compile. The statement about wrap("hi") is false: inference from the argument supplies a candidate of string, and the default is only used when there are no inference candidates, so the result is string.
This fails with "Generic type 'Box' requires 1 type argument(s)". Which line is wrong?#
interface Box<T> {
value: T;
}
const b: Box = { value: 1 };Show answer
The bug is on line 4.
Box is generic with no default, so it must be supplied a type argument: const b: Box<number> = { value: 1 }. Alternatively give T a default (interface Box<T = unknown>).
What problem do generics solve? Give an example.#
Show answer
Generics let you write reusable code that preserves the relationship between input and output types instead of widening to any. For example function identity<T>(x: T): T returns exactly the type it was given, so identity('hi') is typed as string, keeping type safety while staying reusable.
Generics parameterise types the way function arguments parameterise values: one definition works for many types while preserving precise type information end to end.
A default type parameter makes a type argument optional. In interface Container<T _____ string> { value: T; }, writing Container with no argument resolves T to _____.#
Show answer
A default type parameter makes a type argument optional. In interface Container<T **=** string> { value: T; }, writing Container with no argument resolves T to **string**.
<T = string> gives T a default, so Container (no type argument) is equivalent to Container<string>. Defaults must follow any required type parameters, mirroring default function arguments.
True or false: the TypeScript generic type alias declaration type Pair<A = number, B> = [A, B]; compiles without errors.#
Options
Show answer
A generic type alias declared as type Pair<A = number, B> = [A, B]; does not compile in TypeScript. A required type parameter cannot follow one that has a default, so defaulted parameters must come last, as in type Pair<B, A = number>.
This is false. TypeScript requires that once a type parameter has a default, every type parameter after it must also have a default; otherwise the compiler reports 'Required type parameters may not follow optional type parameters'. The rule applies to type aliases, interfaces, classes, and functions alike. Here A has a default and B does not, so the declaration is rejected. Reordering to type Pair<B, A = number> = [A, B]; fixes it, because the required parameter now comes first and the defaulted one comes last.
In TypeScript 5.x, given declare function make<T extends object = { id: number }>(): T;, what type does the statement make(); infer for T, and what rule determines it when there are no inference candidates and no contextual type?#
Show answer
T is { id: number }. When a call produces no inference candidates for a type parameter (here there are no arguments, and the bare expression statement gives no contextual return type), TypeScript uses the type parameter's default if one is declared. The constraint object is only the fallback used when no default exists, and unknown is used when neither exists.
Type-argument resolution for a call with no candidates follows a fixed order: explicit type argument, then inferred candidates, then the default type, then the constraint, then unknown. Here T has no argument-based candidates and the call sits in an expression statement with no contextual type, so the default { id: number } wins over the constraint object. If the call were written as const x: Foo = make();, the contextual return type would supply an inference candidate and override the default.
Sources
The official documentation these questions are checked against:
Related interview questions
Job market
See typescript salaries and hiring demand from live job postings.
The other 6 questions
This page shows 10 and marks what you pick. That's as far as a page can go. A free account opens the other 6 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.
Free · the whole bank · 100 marked answers per 30 days · written feedback on the paid plan