0
0
Typescriptprogramming~10 mins

Generic interface declaration in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic interface declaration
Start
Declare generic interface with <T>
Use <T> as type placeholder inside interface
Create object implementing interface with specific type
Access properties with concrete type
End
This flow shows how a generic interface is declared with a type placeholder and then used with a specific type.
Execution Sample
Typescript
interface Box<T> {
  content: T;
}

const box: Box<string> = { content: "apple" };
console.log(box.content);
Defines a generic interface Box with a type T, then creates a Box of string and logs its content.
Execution Table
StepActionEvaluationResult
1Declare interface Box<T>T is a placeholder typeInterface Box expects a type parameter T
2Create object box of type Box<string>T is replaced by stringbox has property content of type string
3Assign { content: "apple" } to boxcontent is string, matches typebox.content = "apple"
4console.log(box.content)Output the content propertyPrints: apple
5End of executionNo errorsProgram ends successfully
💡 All steps complete, generic type replaced by string, output printed
Variable Tracker
VariableStartAfter 1After 2After 3Final
Tplaceholderplaceholderstringstringstring
boxundefinedundefinedBox<string>{ content: "apple" }{ content: "apple" }
box.contentundefinedundefinedundefined"apple""apple"
Key Moments - 3 Insights
Why do we write <T> after interface name?
The <T> declares a placeholder type that can be replaced later with any specific type, as shown in step 1 of the execution_table.
What happens when we write Box<string>?
Box<string> replaces the placeholder T with string, so content must be a string, as seen in step 2 and 3.
Can box.content be a number here?
No, because box is Box<string>, content must be string. Assigning a number would cause a type error, which is prevented by TypeScript.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type does T represent after step 2?
Astring
Bnumber
Cboolean
Dundefined
💡 Hint
Check the 'Evaluation' column at step 2 where T is replaced by string.
At which step is the value "apple" assigned to box.content?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns to find when box.content gets "apple".
If we changed Box<string> to Box<number>, what would change in variable_tracker?
AT would be number but box.content would stay string
BT would stay string but box.content would be number
CT would be number and box.content would be a number
DNo change at all
💡 Hint
Refer to how T and box.content change together in variable_tracker.
Concept Snapshot
Generic interface syntax:
interface InterfaceName<T> { property: T; }

Use with specific type:
const obj: InterfaceName<Type> = { property: value };

T is a placeholder replaced by the given type.
Ensures type safety and flexibility.
Full Transcript
This example shows how to declare a generic interface in TypeScript using a placeholder type T. The interface Box<T> has a property content of type T. When we create an object box of type Box<string>, T is replaced by string, so content must be a string. We assign the string "apple" to box.content and print it. The execution table traces each step, showing how T changes from a placeholder to string, and how box.content gets the value. Key moments clarify why we use <T> and how the type replacement works. The quiz tests understanding of type substitution and assignment. The snapshot summarizes the syntax and behavior of generic interfaces.