How to Create an Interface with Condtional Type ?
Last Updated :
29 May, 2024
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 define interfaces that dynamically adjust their structure and constraints, providing powerful tools for building robust and type-safe applications.
Using extends keyword with interfaces
This approach involves using conditional types to discriminate between different types and define interface properties accordingly to conditionally extend the interface.
Syntax:
interface InterfaceName<T> {
propertyName:
T extends ConditionType ? TypeIfTrue : TypeIfFalse;
}
Example: The below code creates an interface with conditional type using extends keyword and ternary operator.
JavaScript
interface Vehicle<T> {
type: T extends 'car' ?
'four-wheeler' : 'two-wheeler';
wheels: T extends 'car' ? 4 : 2;
}
const car: Vehicle<'car'> = {
type: 'four-wheeler',
wheels: 4
};
const bike: Vehicle<'bike'> = {
type: 'two-wheeler',
wheels: 2
};
console.log(car);
console.log(bike);
Output:
{ type: four-wheeler, wheels: 4 }
{ type: two-wheeler, wheels: 2 }
Using extends keyword with Mapped Types
Mapped types with conditional constraints transform existing types into new interfaces based on certain conditions.
Syntax:
type MappedTypeName<T> = {
[P in T]:
T extends ConditionType ? TypeIfTrue : TypeIfFalse;
};
Example: The below code implements the mapped types to create an interface with conditional types.
JavaScript
type Fruit = 'apple' | 'banana';
type FruitProperties<T extends string[]> = {
[P in T[number]]: P extends 'apple' ?
{ color: string } : { length: number };
};
interface FruitInfo extends
FruitProperties<Fruit[]> { }
const appleInfo: FruitInfo = {
apple: { color: 'red' }
} as FruitInfo;
const bananaInfo: FruitInfo = {
banana: { length: 10 }
} as FruitInfo;
console.log(appleInfo);
console.log(bananaInfo);
Output:
{ apple: { color: red } }
{ banana: { length: 10 } }
Using Conditional Types with Union Types:
In this approach, we leverage union types along with conditional types to define interfaces that adapt based on different types.
Syntax:
type ConditionalInterface<T> = T extends ConditionType1 ? InterfaceType1 :
T extends ConditionType2 ? InterfaceType2 :
DefaultInterfaceType;
Example: The following code demonstrates the usage of conditional types with union types to define interfaces dynamically.
JavaScript
type Fruit = 'apple' | 'banana';
type FruitProperties<T> = T extends 'apple' ? { color: string } :
T extends 'banana' ? { length: number } :
never;
const appleProperties: FruitProperties<'apple'> = { color: 'red' };
const bananaProperties: FruitProperties<'banana'> = { length: 10 };
console.log(appleProperties); // Output: { color: 'red' }
console.log(bananaProperties); // Output: { length: 10 }
Output
{ color: 'red' }
{ length: 10 }
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 use Interface with Class in TypeScript ? In TypeScript, interfaces define the structure that classes must adhere to, ensuring consistent object shapes and facilitating type-checking.Interfaces declare properties and methods without implementations, serving as contracts for classes to implement.Classes use the implements keyword to adhere t
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
What are type aliases and how to create it in Typescript ? In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name. Aliasing a primitive isn't
3 min read
How to check interface type in TypeScript ? Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an in
2 min read