Skip to content
intermediate

Generic Constraints

Use generic constraints to limit what types can be used.

interface HasLength {
  length: number;
}

function getLength__________...(item: T): number {
  return __________...;
}

// These should work:
getLength("hello");      // string has length
getLength([1, 2, 3]);    // array has length
getLength({ length: 5 }); // object with length

// This would fail type checking:
// getLength(123); // number doesn't have length
0/2 filled tab moves · Ctrl+↵ submits · Ctrl+Shift+↵ runsstarter code