ProgramGuru

Multiple Type Parameters

Understand Multiple Type Parameters

Multiple generic type parameters can preserve relationships among independently typed arguments and return values.

What you will learn: the purpose of multiple type parameters, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how multiple type parameters works and where its type rules apply.
  • Predict the exact output of the multiple type parameters example.
  • Diagnose a common issue: An assertion in this generic implementation compensates for JavaScript’s computed property typing; it is not runtime validation, so keep the implementation constrained and small.

Build the mental model

For a key/value pair, naming K and V separately communicates that the key and value need not share a type. Generic functions can combine related and unrelated type variables without collapsing everything to a union or any.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Multiple Type Parameters: explained visuallyK="retry" → V=number → Record<K,V>K="retry"V=numberRecord<K,V>Follow the values through the operation
Multiple Type Parameters: K="retry" → V=number → Record<K,V>.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

function record<K extends string, V>(key: K, value: V): Record<K, V> {
  return { [key]: value } as Record<K, V>;
}
const settings = record("retry", 3);
console.log(settings.retry);
const flags = record("enabled", true);
console.log(flags.enabled);

Verified output

3
true

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

K captures the specific key retry and V captures number for the first call; the object has retry: 3. The next call independently infers key enabled and boolean value, printing true.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

An assertion in this generic implementation compensates for JavaScript’s computed property typing; it is not runtime validation, so keep the implementation constrained and small.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Build a makeEntry<Key,Value> function returning a typed tuple and try three combinations of types.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What benefit do K and V give a key/value helper?
Choose one answer. You can try again.

Answer and explanation

They preserve distinct key and value types in the result.

Multiple generic type parameters can preserve relationships among independently typed arguments and return values.

Review the key idea

Multiple Type Parameters: Multiple generic type parameters can preserve relationships among independently typed arguments and return values.

K captures the specific key retry and V captures number for the first call; the object has retry: 3. The next call independently infers key enabled and boolean value, printing true.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: The keyof Constraint →