How to Restrict a Generic Interface from Accepting an Unknown Data Type ? Last Updated : 23 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Restricting a generic interface from accepting an unknown data type means specifying constraints on the type parameter to disallow the unknown. This ensures that the interface can only be instantiated with types that are more specific than unknown, enhancing type safety and preventing unintended usage of potentially unsafe data types.Below are the approaches used to restrict a generic interface from accepting an unknown data type:Table of ContentApproach 1: Using conditional typesApproach 2: Using mapped types Approach 1: Using conditional typesUsing conditional types in TypeScript involves creating type definitions that depend on the evaluation of type conditions, enabling conditional logic to determine the resulting type based on specific type constraints and conditions.Syntax:type MyType<T> = T extends string ? number : boolean;Example: In this example we Defines NotUnknown<T> type to allow only string types, ensuring MyInterface's data property holds strings. JavaScript type NotUnknown<T> = T extends string ? T : never; interface MyInterface<T> { data: NotUnknown<T>; } const result: MyInterface<string> = { data: "GeeksforGeeks" }; console.log(result.data); Output:GeeksforGeeksApproach 2: Using mapped types Using mapped types in TypeScript involves transforming types by iterating over their properties and applying specified operations, enabling dynamic creation of new types based on existing ones.Syntax:type MyMappedType<T> = { [K in keyof T]: SomeType; }Example: In this example we Defines a mapped type NotUnknown<T> excluding unknown, ensuring MyInterface's data property holds only specified types. we use string and number both in our data. JavaScript type NotUnknown<T> = { [K in keyof T]: T[K] extends unknown ? never : T[K] }; interface MyInterface<T> { data: NotUnknown<T>; } const result1: MyInterface<number | string> = { data: 18 }; console.log(result1.data); const result2: MyInterface<number | string> = { data: "Virat_Kohli" }; console.log(result2.data); Output:18Virat_Kohli Comment More infoAdvertise with us Next Article How to Restrict a Generic Interface from Accepting an Unknown Data Type ? P parzival_op Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads How to Create an Interface with Generic Type? Creating interfaces with generic types in TypeScript is a powerful feature that enhances code flexibility and reusability. By defining interfaces with generic type parameters, you can create components that work with various data types while maintaining type safety.Interface with a Single Generic Ty 3 min read How to Implement a Generic Function with a Conditional Return Type ? Implementing a generic function with a conditional return type involves creating a function that can accept multiple data types and return different types based on specified conditions. This allows for flexible behavior depending on inputs. The function's return type may vary dynamically, enhancing 4 min read How to Create an Interface with Condtional Type ? Conditional types in TypeScript offer developers the ability to craft interfaces that adapt their behavior based on the types they encounter. This feature enhances code flexibility and adaptability, particularly in situations where types may vary. By employing conditional types, developers can defin 3 min read How to Create Arrays of Generic Interfaces in TypeScript ? In TypeScript, managing data structures effectively is crucial for building robust applications. Arrays of generic interfaces provide a powerful mechanism to handle varied data types while maintaining type safety and flexibility. There are various methods for constructing arrays of generic interface 3 min read How to Define a Generic Type for an Array in TypeScript ? Generics in TypeScript are a way to write reusable code that can work with different types of data. When it comes to defining generic types for arrays. TypeScript provides flexible options to ensure type safety while allowing for flexibility in the types of elements stored in the array.Table of Cont 3 min read Like