Generic interfaces let you create flexible blueprints that work with many types, not just one. This helps you write code that can handle different data without repeating yourself.
Generic interface declaration in Typescript
interface InterfaceName<T> {
property: T;
method(param: T): T;
}The <T> after the interface name means it uses a generic type called T.
You can use any name instead of T, but T is common and stands for 'Type'.
Box holds one property content of any type T.interface Box<T> {
content: T;
}Pair uses two generic types T and U for two properties.interface Pair<T, U> {
first: T;
second: U;
}Result has a boolean success and a generic data of type T.interface Result<T> {
success: boolean;
data: T;
}This program defines a generic interface Container with a type T. Then it creates a class NumberContainer that uses number as the type. It stores a number and returns it with getValue. Finally, it prints the stored number.
interface Container<T> {
value: T;
getValue(): T;
}
class NumberContainer implements Container<number> {
value: number;
constructor(value: number) {
this.value = value;
}
getValue(): number {
return this.value;
}
}
const myNumber = new NumberContainer(42);
console.log(myNumber.getValue());Generic interfaces help keep your code DRY (Don't Repeat Yourself) by reusing the same structure for different types.
You can use multiple generic types separated by commas, like <T, U>.
When implementing a generic interface, you must specify the actual type to use.
Generic interfaces let you create flexible and reusable blueprints for data.
Use <T> to declare a generic type in an interface.
Implementations must specify the actual type to use with the generic interface.
Practice
<T> represent in a TypeScript generic interface declaration?Solution
Step 1: Understand generic syntax
The<T>syntax in TypeScript is used to declare a generic type parameter, which acts as a placeholder for a type.Step 2: Identify the role of
This placeholder allows the interface to be flexible and reusable with different types specified later.TFinal Answer:
A placeholder for a type that will be specified later -> Option DQuick Check:
Generic<T>= Type placeholder [OK]
<T> means 'type to be given later' [OK]- Thinking
Tis a variable or constant - Confusing
<T>with a function parameter - Assuming
Tis always a string type
Box with a type parameter T?Solution
Step 1: Check interface declaration syntax
In TypeScript, interfaces are declared with the keywordinterfacefollowed by the name and optional generic parameters in angle brackets.Step 2: Verify correct use of braces and type parameter
The correct syntax includes curly braces to define the interface body and uses<T>to declare the generic type parameter.Final Answer:
interface Box<T> { content: T; } -> Option BQuick Check:
Generic interface syntax = interface Box<T> { content: T; } [OK]
<T> and braces {} [OK]- Omitting the generic parameter
<T> - Missing curly braces around interface body
- Using equals sign instead of braces
interface Container<T> { value: T; }What is the output of this code?
const numberContainer: Container<number> = { value: 42 };
console.log(numberContainer.value);Solution
Step 1: Understand the generic interface usage
The interfaceContainer<T>expects a type parameter. Here,numberis specified, sovaluemust be a number.Step 2: Analyze the assigned object and console output
The object{ value: 42 }matches the interface withvalueas a number. LoggingnumberContainer.valueprints42.Final Answer:
42 -> Option CQuick Check:
Generic with number type outputs 42 [OK]
- Expecting a runtime error due to generics
- Confusing generic type with runtime type enforcement
- Assuming output is undefined or null
interface Pair<T> {
first: T;
second: T;
}
const pair: Pair = { first: 'hello', second: 'world' };Solution
Step 1: Check interface usage with generics
The interfacePair<T>requires a type argument when used. Here,Pairis used without specifying<T>.Step 2: Identify the missing generic argument error
TypeScript will report an error because the generic type parameter is missing when declaringpair.Final Answer:
Missing generic type argument when using Pair -> Option AQuick Check:
Generic interface usage needs type argument [OK]
- Forgetting to add
<Type>when using generic interfaces - Assuming default type if omitted
- Confusing property types with generic parameter
Result<T> that holds a data property of type T and a success boolean. Which of the following implementations correctly uses this interface to type a variable holding a string result?Solution
Step 1: Understand the generic interface requirements
The interfaceResult<T>expects adataproperty of typeTand asuccessboolean.Step 2: Check each option for correct type usage
const res: Result<string> = { data: 123, success: true }; assigns a number todatabut expects a string type, so it's incorrect.
const res: Result<string> = { data: 'Done', success: true }; correctly assigns a string todataand includessuccess.
const res: Result = { data: 'Done', success: true }; omits the generic type argument, which is required.
const res: Result<number> = { data: 'Done', success: true }; usesnumbertype but assigns a string, causing a type mismatch.Final Answer:
const res: Result<string> = { data: 'Done', success: true }; -> Option AQuick Check:
Generic type matches property type = const res: Result<string> = { data: 'Done', success: true }; [OK]
- Assigning wrong type to generic property
- Omitting generic type argument
- Mixing up generic type parameters
Ready to prove it?
Pass the test and this pattern moves unknown
