Tuple Patterns

👨‍💼 Tuples are excellent for function return values when you need to return multiple things.
function divide(a: number, b: number): [number, boolean] {
	if (b === 0) {
		return [0, false] // Result and success flag
	}
	return [a / b, true]
}
🐨 Open and:
  1. Create a function that returns [value, success] tuple
  2. Create a function that returns [min, max] from an array
  3. Access the tuple values by index (no destructuring)
💰 Return tuple pattern:
function getMinMax(nums: Array<number>): [number, number] {
	let min = nums[0]
	let max = nums[0]
	for (const num of nums) {
		if (num < min) min = num
		if (num > max) max = num
	}
	return [min, max]
}

Please set the playground first

Loading "Tuple Patterns"
Loading "Tuple Patterns"