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
Slices & Maps → Arrays & Objects
MirrorLesson 7 of 10
Lesson 7

Slices & Maps → Arrays & Objects

Working with collections of data

Introduction

In this lesson, you'll learn about slices & maps → arrays & objects 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 working with collections of data.

TS
In TypeScript:

TypeScript has its own approach to working with collections of data, 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
// Array
const nums: number[] = [1, 2, 3, 4, 5];
nums.push(6);
const sub = nums.slice(1, 4); // [2, 3, 4]

// Object as map
const ages: Record<string, number> = {
  Alice: 30,
  Bob: 25,
};
ages["Charlie"] = 35;
delete ages["Bob"];
const val = ages["Alice"]; // undefined if missing

// Map (proper hash map)
const agesMap = new Map<string, number>([["Alice", 30]]);
agesMap.set("Charlie", 35);

// Iteration
nums.forEach((v, i) => console.log(i, v));
Object.entries(ages).forEach(([k, v]) => console.log(k, v));

Comparing to Go

Here's how you might have written similar code in Go:

GO
Go (What you know)
// Slice
nums := []int{1, 2, 3, 4, 5}
nums = append(nums, 6)
sub := nums[1:4] // [2, 3, 4]

// Map
ages := map[string]int{
    "Alice": 30,
    "Bob":   25,
}
ages["Charlie"] = 35
delete(ages, "Bob")
val, ok := ages["Alice"]

// Range
for i, v := range nums {
    fmt.Println(i, v)
}
for k, v := range ages {
    fmt.Println(k, v)
}
Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go slices → TypeScript arrays (both dynamic)

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go map[K]V → TypeScript Map<K,V> or Record<K,V>

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go delete(m, k) → TypeScript delete m[k] or map.delete(k)

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go range → TypeScript forEach/for...of

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go has typed nil for missing map keys; TypeScript returns undefined

Step-by-Step Breakdown

1. Slice vs Array

Go slices grow with append; TypeScript arrays grow with push. Both support slicing with index ranges.

GO
Go
nums = append(nums, 6)
sub := nums[1:4]
TS
TypeScript
nums.push(6);
const sub = nums.slice(1, 4);

2. Maps

TypeScript has two options: plain objects (Record) for string keys, or Map<K,V> for any key type. Go's map is always typed.

GO
Go
m := map[string]int{"a": 1}
v, ok := m["a"]
TS
TypeScript
const m = new Map<string, number>([["a", 1]]);
const v = m.get("a"); // undefined if missing

3. Functional Methods

TypeScript arrays have rich functional methods (map, filter, reduce) that Go doesn't have built-in — you write loops or use Go's slices package.

TS
TypeScript
const doubled = nums.map(n => n * 2);
const evens = nums.filter(n => n % 2 === 0);
const total = nums.reduce((acc, n) => acc + n, 0);
Rule of Thumb
Prefer functional methods (map/filter/reduce) in TypeScript over imperative loops.

Common Mistakes

When coming from Go, developers often make these mistakes:

  • Go slices → TypeScript arrays (both dynamic)
  • Go map[K]V → TypeScript Map<K,V> or Record<K,V>
  • Go delete(m, k) → TypeScript delete m[k] or map.delete(k)
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

  • Go slice → TypeScript array (push/slice)
  • Go map → TypeScript Map or Record
  • TypeScript has built-in map/filter/reduce
  • Both support range/forEach iteration
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