MediumGenerics & Bounds💻 Output Question

Generic identity — T resolves to the argument type

💡

Hint

Generic functions preserve the exact type of their argument through the return type. This is why identity<T>(v: T): T is so powerful — callers get back exactly the type they put in, not a wider type.

What does this output?

// Simulate generic type resolution
function identity(value) {
  return value;
}

// TypeScript resolves T to the argument type at each call site
const n = identity(42);       // T = number
const s = identity('hello');  // T = string
const a = identity([1, 2, 3]); // T = number[]
const o = identity({ x: 1 }); // T = { x: number }

console.log(typeof n);
console.log(typeof s);
console.log(Array.isArray(a));
console.log(typeof o);
console.log(n + 1);  // safe — TypeScript knows it's number

Correct Output

number
string
true
object
43

Why this output?

Explanation: Each call resolves T to the exact type of the argument. TypeScript does not widen — identity(42) gives T=number, not T=number|string.

Key Insight: Generic functions preserve the exact type of their argument through the return type. This is why identity(v: T): T is so powerful — callers get back exactly the type they put in, not a wider type.

More Generics & Bounds Output Questions

Mediumextends constraint — keyof and property accessHardGeneric Array utilities — map and filter type resolutionHardConstrained generic with default — what type is inferred

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz