0
0
Typescriptprogramming~10 mins

Generic constraints with extends in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic constraints with extends
Define generic type T
Add constraint: T extends SomeType
Use T in function/class
Call function with argument
Check if argument matches constraint
Run code
This flow shows how a generic type T is constrained to extend a specific type, ensuring only compatible types are used.
Execution Sample
Typescript
interface HasName { name: string }
function greet<T extends HasName>(obj: T): string {
  return `Hello, ${obj.name}`;
}

const person = { name: "Alice", age: 30 };
console.log(greet(person));
This code defines a generic function that only accepts objects with a 'name' property, then greets the person.
Execution Table
StepActionEvaluationResult
1Define interface HasNameHasName requires 'name' stringInterface ready
2Define function greet with T extends HasNameT must have 'name' propertyFunction ready
3Create object person{ name: 'Alice', age: 30 }Object created
4Call greet(person)Check if person fits T extends HasNameperson has 'name', OK
5Return greeting string`Hello, Alice`Output: Hello, Alice
6Print outputConsole logs greetingHello, Alice
💡 Function call succeeds because person matches constraint HasName
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
personundefinedundefinedundefined{ name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }{ name: 'Alice', age: 30 }
greet returnundefinedundefinedundefinedundefined`Hello, Alice``Hello, Alice`
Key Moments - 2 Insights
Why does greet(person) work even though person has an extra 'age' property?
Because the constraint only requires 'name' property. Extra properties do not cause errors as long as the required ones exist (see step 4 in execution_table).
What happens if we call greet with an object missing 'name'?
TypeScript will give a compile-time error because the object does not satisfy the constraint T extends HasName (not shown in execution_table but implied in flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 5?
A"Hello, Alice"
B"Hello, person"
C"Hello, undefined"
DError
💡 Hint
Check the 'Result' column at step 5 in execution_table
At which step does TypeScript check if the argument matches the constraint?
AStep 2
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table for step 4
If the object passed to greet did not have a 'name' property, what would happen?
AThe function would print 'Hello, undefined'
BThe function would run but return an empty string
CTypeScript would give a compile-time error
DThe function would ignore the missing property and run
💡 Hint
Refer to key_moments about constraint violations and TypeScript errors
Concept Snapshot
Generic constraints with extends:
- Use <T extends Type> to restrict T
- T must have all properties of Type
- Allows safe access to those properties
- Extra properties are allowed
- Compile-time error if constraint not met
Full Transcript
This example shows how to use generic constraints with extends in TypeScript. We define an interface HasName requiring a 'name' string property. Then we create a generic function greet that only accepts types extending HasName. When we call greet with an object having 'name' and 'age', it works because the object meets the constraint. The execution table traces each step: defining interface, function, creating object, calling function, checking constraint, returning greeting, and printing output. Key moments clarify why extra properties are allowed and what happens if the constraint is not met. The visual quiz tests understanding of output, constraint checking step, and error behavior. The snapshot summarizes the main points about generic constraints with extends.