0 of 5 problems solved0%
3 of 5

Min by Length

A constraint limits what a type parameter can be. Use T extends { length: number } so you can safely read .length. This allows strings, arrays, and any object with a numeric length.

Pick the shorter of two values and return it as the same T so callers keep the exact type.

function shorter<T extends { length: number }>(a: T, b: T) {
  return a.length <= b.length ? a : b;
}

Your Task

Return the shorter of two length-bearing values.

  • Implement function minByLength<T extends { length: number }>(a: T, b: T): T.
  • Compare a.length and b.length and return the shorter (return a on a tie).
  • Do not convert values; keep them as T.