Interfaces — Both Use Structural Typing
Structural typing: satisfy an interface by having the right shape
Introduction
In this lesson, you'll learn about interfaces — both use structural typing in TypeScript. Coming from Go, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Go, you're familiar with structural typing: satisfy an interface by having the right shape.
TypeScript has its own approach to structural typing: satisfy an interface by having the right shape, which we'll explore step by step.
The TypeScript Way
Let's see how TypeScript handles this concept. Here's a typical example:
interface Stringer {
toString(): string;
}
class Dog {
constructor(public name: string) {}
toString(): string { return "Dog: " + this.name; }
}
class Cat {
constructor(public name: string) {}
toString(): string { return "Cat: " + this.name; }
}
// Dog and Cat satisfy Stringer structurally
function print(s: Stringer): void {
console.log(s.toString());
}
print(new Dog("Rex"));
print(new Cat("Whiskers"));Comparing to Go
Here's how you might have written similar code in Go:
type Stringer interface {
String() string
}
type Dog struct{ Name string }
func (d Dog) String() string { return "Dog: " + d.Name }
type Cat struct{ Name string }
func (c Cat) String() string { return "Cat: " + c.Name }
// Dog and Cat satisfy Stringer without declaring it
func print(s Stringer) {
fmt.Println(s.String())
}
print(Dog{Name: "Rex"})
print(Cat{Name: "Whiskers"})You may be used to different syntax or behavior.
Both Go and TypeScript use structural typing — no explicit 'implements' needed
You may be used to different syntax or behavior.
TypeScript also supports explicit 'implements' for documentation clarity
You may be used to different syntax or behavior.
Go interfaces are the primary abstraction; TypeScript also has abstract classes
You may be used to different syntax or behavior.
TypeScript interfaces can extend multiple other interfaces
Step-by-Step Breakdown
1. Implicit Satisfaction
In both Go and TypeScript, if a type has the required methods, it satisfies the interface automatically — no 'implements' declaration needed.
2. Explicit implements
TypeScript allows optional explicit 'implements' which serves as a compile-time check that the class fulfills the contract.
class Dog implements Stringer {
constructor(public name: string) {}
toString(): string { return "Dog: " + this.name; }
}3. Interface Composition
Both languages support composing interfaces from smaller ones.
type ReadWriter interface {
Reader
Writer
}interface ReadWriter extends Reader, Writer {}Common Mistakes
When coming from Go, developers often make these mistakes:
- Both Go and TypeScript use structural typing — no explicit 'implements' needed
- TypeScript also supports explicit 'implements' for documentation clarity
- Go interfaces are the primary abstraction; TypeScript also has abstract classes
Key Takeaways
- Both Go and TypeScript use structural typing
- No 'implements' required in Go; optional in TypeScript
- TypeScript interface extends; Go interface embedding
- Interfaces are the key abstraction in both languages