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;
}
Return the shorter of two length-bearing values.