Visit complete TypeScript roadmap
TypeScript Topic

Generic Constraints

Generic Constraints

Generic constraints in TypeScript allow you to specify the requirements for the type parameters used in a generic type. These constraints ensure that the type parameter used in a generic type meets certain requirements.

Constraints are specified using the extends keyword, followed by the type that the type parameter must extend or implement.

interface Lengthwise {
  length: number;
}

function loggingIdentity<T extends Lengthwise>(arg: T): T {
  // Now we know it has a .length property, so no more error
  console.log(arg.length);

  return arg;
}

loggingIdentity(3); // Error, number doesn't have a .length property
loggingIdentity({ length: 10, value: 3 }); // OK

In this example, the Lengthwise interface defines a length property. The loggingIdentity function uses a generic type parameter T that is constrained by the Lengthwise interface, meaning that the type parameter must extend or implement the Lengthwise interface. This constraint ensures that the length property is available on the argument passed to the loggingIdentity function.

Learn more from the following resources:

More Topics

Explore related content

View All Topics
Loved by 100K+ Developers

Start Your Learning
Journey Today

Join thousands of developers who are leveling up their skills with structured roadmaps and expert guidance

No credit card required
Always free
Track your progress