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.
In Go, you're familiar with working with collections of data.
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:
// 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:
// 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)
}You may be used to different syntax or behavior.
Go slices → TypeScript arrays (both dynamic)
You may be used to different syntax or behavior.
Go map[K]V → TypeScript Map<K,V> or Record<K,V>
You may be used to different syntax or behavior.
Go delete(m, k) → TypeScript delete m[k] or map.delete(k)
You may be used to different syntax or behavior.
Go range → TypeScript forEach/for...of
You may be used to different syntax or behavior.
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.
nums = append(nums, 6)
sub := nums[1:4]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.
m := map[string]int{"a": 1}
v, ok := m["a"]const m = new Map<string, number>([["a", 1]]);
const v = m.get("a"); // undefined if missing3. 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.
const doubled = nums.map(n => n * 2);
const evens = nums.filter(n => n % 2 === 0);
const total = nums.reduce((acc, n) => acc + n, 0);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)
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