typescript named return 1

typescript named return

// The closest thing to a named return is
// defining a type and using it immediately
type Total = number
type Product = number
function stats(vals: number[]): [Total, Product] {
	...
}

Here is what the above code is Doing:
1. Define a type called Total
2. Define a type called Product
3. Define a function called stats that takes an array of numbers and returns a tuple of two numbers
4. The first number in the tuple is the total of all the numbers in the array
5. The second number in the tuple is the product of all the numbers in the array

Similar Posts