GO
TS

Go to TypeScript

10 lessons

Progress0%
1Variables & Types2Functions3Structs & Classes4Interfaces — Both Use Structural Typing5Error Handling6Goroutines vs Async/Await7Slices & Maps → Arrays & Objects8Packages & Modules9Testing10Generics
All Mirror Courses
GO
TS
Interfaces — Both Use Structural Typing
MirrorLesson 4 of 10
Lesson 4

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.

Mirror Card
GO
From Go:

In Go, you're familiar with structural typing: satisfy an interface by having the right shape.

TS
In TypeScript:

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:

TS
TypeScript 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:

GO
Go (What you know)
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"})
Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Both Go and TypeScript use structural typing — no explicit 'implements' needed

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

TypeScript also supports explicit 'implements' for documentation clarity

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go interfaces are the primary abstraction; TypeScript also has abstract classes

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

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.

Rule of Thumb
This is called structural (or duck) typing: if it walks like a duck and quacks like a duck, it is a duck.

2. Explicit implements

TypeScript allows optional explicit 'implements' which serves as a compile-time check that the class fulfills the contract.

TS
TypeScript
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.

GO
Go
type ReadWriter interface {
    Reader
    Writer
}
TS
TypeScript
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
Common Pitfall
Don't assume TypeScript works exactly like Go. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Go code in TypeScript to practice these concepts.
PreviousNext