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]
}
- Create a function that returns
[value, success]tuple - Create a function that returns
[min, max]from an array - 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]
}