TypeScript Constraints Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report TypeScript constraints are used in generic type declarations to restrict the types that can be used as type arguments for the generic. Constraints allow you to specify that a generic type parameter must meet certain criteria, such as implementing a particular interface, having a specific method, or extending a specific class. This ensures type safety and enhances code predictability. Syntax: function functionName<T extends ConstraintType>(param: T): ReturnType { // Function implementation}Parameters:T is the generic type parameter.extends is used to specify the constraint.ConstraintType is the type or interface that T must extend or implement.param is the parameter that must be of type T.ReturnType is the return type of the function.Example 1: In this example, we are getting some parameters and we are restricting that parameter, which means the parameter value can be anything other than a number. and it returns the output according to their length method. JavaScript function whatIsLength<Type extends { length: number }> (x: Type, y: Type) { if (x.length >= y.length) { return ("x is larger than y"); } else { return ("x is smaller than y");; } } const ans = whatIsLength([4, 0], [8, 9, 0]); console.log(ans); const ans1 = whatIsLength("abcde", "bb"); console.log(ans1); Output: "x is smaller than y" "x is larger than y"Example 2: In this example, we are giving numbers as a parameter and it is throwing error as numbers do not have length method. this is how we can limit the type. JavaScript function whatIsLength<Type extends { length: number }> (x: Type, y: Type) { if (x.length >= y.length) { return ("x is larger than y"); } else { return ("x is smaller than y");; } } const ans = whatIsLength([4, 0], [8, 9, 0]); console.log(ans); const ans1 = whatIsLength("abcde", "bb"); console.log(ans1); // Errror as values are number // and they do not have length method const ans2 = whatIsLength(10, 12); console.log(ans2); Output: "x is smaller than y""x is larger than y"Argument of type 'number' is not assignable to parameter of type '{ length: number; }'. Comment More infoAdvertise with us Next Article TypeScript Constraints S sahoopratyushkumar3 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads TypeScript Generic Constraints In TypeScript, generic constraints restrict the types that can be used with a generic type by using the extends keyword. This ensures that the generic type adheres to a specific structure or interface, allowing access to certain properties or methods within the generic type.What are Generic Constrai 3 min read TypeScript Conditional Types In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions.They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.Conditiona 4 min read Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 3 min read DBMS Integrity Constraints Integrity constraints are a set of rules used in DBMS to ensure that the data in a database is accurate, consistent and reliable. These rules helps in maintaining the quality of data by ensuring that the processes like adding, updating or deleting information do not harm the integrity of the databas 8 min read Interfaces in TypeScript TypeScript is a statically typed superset of JavaScript that adds optional types, classes, interfaces, and other features to help developers build robust and maintainable applications. One of the most powerful features of TypeScript is interfaces, which allow you to define the structure of objects, 4 min read Like