HardGenerics & Bounds💻 Output Question

Generic Array utilities — map and filter type resolution

💡

Hint

Array.prototype.map<U> and filter both use generics. map<T,U>(fn: (v: T) => U): U[] transforms the element type. filter(pred): T[] keeps the original type. TypeScript resolves each generic at the call site.

What does this output?

// TypeScript generic type resolution for array utilities
// map<T, U>(arr: T[], fn: (x: T) => U): U[]
// filter<T>(arr: T[], pred: (x: T) => boolean): T[]

const numbers = [1, 2, 3, 4, 5];
const strings = ['apple', 'banana', 'cherry'];

// map: T=number, U=string  → string[]
const formatted = numbers.map(n => n.toFixed(1));
console.log(formatted.join(','));

// filter: T=number → number[]
const evens = numbers.filter(n => n % 2 === 0);
console.log(evens.join(','));

// map + filter chain: T=string → string[]
const long = strings
  .filter(s => s.length > 5)
  .map(s => s.toUpperCase());
console.log(long.join(','));

// TypeScript resolves chained generics — each step keeps correct type
console.log(typeof evens[0]);

Correct Output

1.0,2.0,3.0,4.0,5.0
2,4
BANANA,CHERRY
number

Why this output?

Explanation: map preserves element type through the callback return type. filter preserves the original array element type. Chained operations each resolve their generics independently.

Key Insight: Array.prototype.map and filter both use generics. map(fn: (v: T) => U): U[] transforms the element type. filter(pred): T[] keeps the original type. TypeScript resolves each generic at the call site.

More Generics & Bounds Output Questions

MediumGeneric identity — T resolves to the argument typeMediumextends constraint — keyof and property accessHardConstrained generic with default — what type is inferred

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz