A generic interface is a way to define an interface with a placeholder type that can be specified later, allowing the interface to work with different data types while keeping type safety.
Click to reveal answer
beginner
How do you declare a generic interface with one type parameter named <T>?
You write: interface MyInterface<T> { value: T; } This means the interface has a property 'value' of type T, which will be specified when used.
Click to reveal answer
beginner
Why use generic interfaces instead of fixed types?
Generic interfaces let you write flexible and reusable code that works with many types without losing the benefits of type checking.
Click to reveal answer
beginner
Example: What does this interface mean? interface Box<T> { content: T; }
It means 'Box' can hold any type of content. When you use it, you specify the type, like Box<string> for a box holding strings or Box<number> for numbers.
Click to reveal answer
intermediate
Can a generic interface have multiple type parameters? How?
Yes. You separate them with commas inside the angle brackets. For example: interface Pair<T, U> { first: T; second: U; }
Click to reveal answer
How do you declare a generic interface named 'Container' with one type parameter 'T'?
Ainterface Container { item: T; }
Binterface Container<T> { item: any; }
Cinterface Container<T> { item: T; }
Dinterface Container<T> { item: string; }
✗ Incorrect
Option C correctly declares a generic interface with a type parameter T used for the item property.
What is the benefit of using a generic interface?
AIt allows the interface to work with any type while keeping type safety.
BIt makes the interface only work with strings.
CIt disables type checking.
DIt makes the interface slower.
✗ Incorrect
Generic interfaces provide flexibility and type safety by allowing any type to be specified.
Which syntax correctly declares a generic interface with two type parameters?
Ainterface Pair<T; U> { first: T; second: U; }
Binterface Pair<T, U> { first: T; second: U; }
Cinterface Pair<T U> { first: T; second: U; }
Dinterface Pair { first: T; second: U; }
✗ Incorrect
Option B correctly uses commas to separate multiple type parameters inside angle brackets.
If you have interface Box<T> { content: T; }, what is the type of 'content' in let box: Box<number>;?
Anumber
Bstring
Cany
DT
✗ Incorrect
When Box is used with number, T becomes number, so content is of type number.
Can you use a generic interface without specifying the type parameter?
AYes, it defaults to 'any'.
BNo, generic interfaces cannot be used.
CYes, it defaults to 'string'.
DNo, you must specify the type parameter.
✗ Incorrect
Option D is correct. Generic type 'Box' requires the type parameter to be specified; omitting it results in a TypeScript error.
Explain what a generic interface is and why it is useful in TypeScript.
Think about how you can write one interface that works with many types.
You got /4 concepts.
Write a simple generic interface named 'Result' that holds a value of any type and a boolean indicating success.
Use angle brackets <> to declare the generic type.
You got /4 concepts.
Practice
(1/5)
1. What does the <T> represent in a TypeScript generic interface declaration?
easy
A. A special type that only accepts strings
B. A variable name for a function
C. A keyword to declare a constant
D. A placeholder for a type that will be specified later
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 T
This placeholder allows the interface to be flexible and reusable with different types specified later.
Final Answer:
A placeholder for a type that will be specified later -> Option D
Quick Check:
Generic <T> = Type placeholder [OK]
Hint: Remember <T> means 'type to be given later' [OK]
Common Mistakes:
Thinking T is a variable or constant
Confusing <T> with a function parameter
Assuming T is always a string type
2. Which of the following is the correct syntax to declare a generic interface named Box with a type parameter T?
easy
A. interface Box<T> = { content: T; }
B. interface Box<T> { content: T; }
C. interface Box<T> content: T;
D. interface Box { content: T; }
Solution
Step 1: Check interface declaration syntax
In TypeScript, interfaces are declared with the keyword interface followed 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.
C. Interface Pair cannot have two properties of the same type
D. Object literal syntax is incorrect
Solution
Step 1: Check interface usage with generics
The interface Pair<T> requires a type argument when used. Here, Pair is 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 declaring pair.
Final Answer:
Missing generic type argument when using Pair -> Option A
Quick Check:
Generic interface usage needs type argument [OK]
Hint: Always specify type argument when using generic interfaces [OK]
Common Mistakes:
Forgetting to add <Type> when using generic interfaces
Assuming default type if omitted
Confusing property types with generic parameter
5. You want to create a generic interface 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?
Step 1: Understand the generic interface requirements
The interface Result<T> expects a data property of type T and a success boolean.
Step 2: Check each option for correct type usage
const res: Result<string> = { data: 123, success: true }; assigns a number to data but expects a string type, so it's incorrect. const res: Result<string> = { data: 'Done', success: true }; correctly assigns a string to data and includes success. const res: Result = { data: 'Done', success: true }; omits the generic type argument, which is required. const res: Result<number> = { data: 'Done', success: true }; uses number type but assigns a string, causing a type mismatch.