TypeScript Working with Constrained Values
Last Updated :
28 Apr, 2025
TypeScript Working with Constrained Values specifies how can we deal with Constrained Values. Generic Constraints are used to specify limits to the types that can be used with generic type parameters. This results in type checking and these conditions ensure that variables have a certain type of value before they are used in the context. This check minimizes the occurrence of runtime errors. But there is a common error when working with generic constraints; The problem is that the function promises to return the same kind of object as was passed in, not just some object matching the constraint.
Syntax:
function genericConstraintFunction<T extends GenericConstraintType>(param: T): void {
// ...
}
Where:
- T is the generic type parameter.
- `extends` GenericConstraintType specifies the constraint that Type T should be extending GenericConstraintType type.
Example 1: In this example, we are getting error while using constrained value.
JavaScript
function gfg<Type extends { index: number }>(
obj: Type,
x: number
): Type {
if (obj.index >= x) {
return obj;
} else {
return { index: x };
}
}
Output:

Explanation
It might look like this function is OK - Type is constrained to { index: number }, and the function either returns Type or a value matching that constraint. The problem is that the function promises to return the same kind of object as was passed in, not just some object matching the constraint. If this code were legal, you could write code that definitely wouldn’t work:
Example 2: In this example, we will see how can we correctly use contraint values in Typescript.
JavaScript
interface Sports {
name: string;
}
function printSportName<T extends Sports>(sport: T): void {
console.log(sport.name);
}
let sport: Sports = { name: "baseball" };
printSportName(sport);
Output:

Explanation:
Lets say we have an interface of sports and it contains a property called name of type string. We created a variable sport of type Sports and assigned the name “baseball”. We have a function called printSportName which expects an argument of type T. Now, to ensure that this Type T is of type Sports, we use the keyword ‘extends’ with Sports. This checks, if the arguments passed through the function, are of type Sports before the function execution.
Conclusion:
In this article, we learnt that there is a problem while using Generic Contrains that the function promises to return the same kind of object as was passed in, not just some object matching the constraint.
Similar Reads
TypeScript Working with Union Types In this article, we are going to learn about Working with Union Types in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In Typescript, the compiler will only allow an operation if it is valid for every member of the union. For example, if
3 min read
TypeScript Constraints 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
2 min read
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 Using Type Parameters in Generic Constraints TypeScript Generics allows you to write reusable code that works with different types. In this article, we will explore how to use type parameters in generic constraints to ensure that which types are allowed in generic types or functions. Syntax:function functionName<Type extends ConstraintType
2 min read
Domain constraints in DBMS In DBMS, constraints are the set of rules that ensures that when an authorized user modifies the database they do not disturb the data consistency and the constraints are specified within the DDL commands like "alter" and "create" command. There are several types of constraints available in DBMS and
4 min read