TypeScript ConstructorParameters<Type> Utility Type Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The TypeScript ConstructorParameters<Type> utility type extracts the parameter types from a constructor function Type. It enhances type safety by enabling the creation of instances with correct constructor arguments, ensuring functions use the precise types expected by the constructor.Syntaxtype ConstructorParametersType = ConstructorParameters<Type>;Parameters:ConstructorParametersType: The name of the type that represents the parameter types of the constructor function.Type: The constructor function type from which you want to extract the parameter types.Example 1: Extracting Constructor Parameters for GFG ClassIn this example, we define a class GFG with a constructor that accepts two string parameters. Using ConstructorParameters<typeof GFG>, we extract these parameter types. We then create a createGFG function that utilizes these types to instantiate GFG objects with varying parameter values. JavaScript // Define a class and its constructor class GFG { constructor(name: string, course: string) { this.name = name; this.course = course; } name: string; course: string; } // Extract parameter types of the constructor type GFGConstructorParams = ConstructorParameters<typeof GFG>; // Create a function that creates an instance of the class function createGFG(...params: GFGConstructorParams): GFG { return new GFG(...params); } const GFG1 = createGFG("GeeksforGeeks", "Java"); const GFG2 = createGFG("gfg", "Python"); console.log(GFG1); console.log(GFG2); Output:GFG { name: 'GeeksforGeeks', course: 'Java' } GFG { name: 'gfg', course: 'Python' }Example 2: Extracting Constructor Parameters for Rectangle ClassIn this example, we create a Rectangle class with a constructor accepting two numbers: width and height. By using ConstructorParameters<typeof Rectangle>, we obtain the parameter types [number, number]. We then craft a createRectangle function using these types to generate Rectangle instances with distinct dimensions. JavaScript // Define a class and its constructor class Rectangle { constructor(public width: number, public height: number) { } } // Extract parameter types of the constructor type RectangleConstructorParams = ConstructorParameters<typeof Rectangle>; // Create a function that creates an instance of the class function createRectangle(...params: RectangleConstructorParams): Rectangle { return new Rectangle(...params); } const rect1 = createRectangle(5, 10); const rect2 = createRectangle(8, 6); console.log(rect1); console.log(rect2); Output:Rectangle { width: 5, height: 10 } Rectangle { width: 8, height: 6 } Comment More infoAdvertise with us Next Article TypeScript ConstructorParameters<Type> Utility Type A abhiisaxena09 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads TypeScript ThisParameterType<Type> Utility Type In this article, we are going to learn about ThisParameterType<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the ThisParameterType<Type> utility type is used to extract the type of this pa 3 min read TypeScript InstanceType<Type> Utility Type In this article, we are going to learn about InstanceType<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the InstanceType<Type> utility type is used to extract the instance type of a constr 3 min read TypeScript OmitThisParameter<Type> Utility Type In this article, we are going to learn about OmitThisParameter<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the OmitThisParameter<Type> utility type is used to create a new function type 3 min read Typescript Partial<Type> Utility Type The Partial<Type> utility in TypeScript creates a new type by making all properties of an existing type optional. This allows you to create flexible object types where only some properties are required, streamlining code and reducing redundancy when working with complex data structures.What is 3 min read TypeScript ReturnType <Type> Utility Type The ReturnType<Type> utility type in TypeScript extracts and infers the return type of a given function type. It enhances type safety and reusability by allowing developers to dynamically determine the type of values returned by functions without manually specifying them.Syntaxtype ResultTypeV 3 min read TypeScript Creating Types from Utility Type TypeScript Creating Types from Utility Type increases the code readability and ease of use when using any complex operations and values, there are different ways to do the type creation with the existing ones. TypeScript Creating Types from Utility Type:Generics: It basically allows us to declare th 3 min read TypeScript ThisType<Type> Utility Type The TypeScript ThisType<Type> utility type allows you to define the type of this within a specific object or function context, providing precise type checking for methods and properties accessed through this. The ThisType<Type> utility type is primarily used to provide type annotations f 3 min read Typescript Required<Type> Utility Type TypeScript's Required<Type> utility type creates a new type by making all properties of an existing type mandatory. It removes optionality from each property, ensuring that all fields must be provided, which enhances type safety and reduces potential errors in type definitions.Syntaxtype Requi 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 TypeScript NonNullable<Type> Utility Type In this article, we are going to learn about NonNullable<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the NonNullable<Type> utility is used to create a new type by removing null and undef 2 min read Like