
Spread Syntax
Like the finest wines and cheeses, TypeScript’s tuples pair beautifully with JavaScript’s spread syntax. This is most useful for function calls that use lots of arguments, like this:
function gpsNavigate(
startLatitudeDegrees: number,
startLatitudeMinutes: number,
startNorthOrSouth: string,
startLongitudeDegrees: number,
startLongitudeMinutes: number,
startEastOrWest: string,
endLatitudeDegrees: number,
endLatitudeMinutes: number,
endNorthOrSouth: string,
endLongitudeDegrees: number,
endLongitudeMinutes: number,
endEastOrWest: string
) {
/* navigation subroutine here */
}
The function call gpsNavigate(40, 43.2, 'N', 73, 59.8, 'W', 25, 0, 'N', 71, 0, 'W') calculates a route from the Codecademy offices in New York City (40 degrees 43.2 minutes north, 73 degrees 59.8 minutes west) to selected coordinates in the Bermuda Triangle. We all agree that this function call is awkward to read.
Instead, we can use tuple variables that represent the starting and ending coordinates:
let c-odecademyCoordinates: [number, number, string, number, number, string] = [40, 43.2, 'N', 73, 59.8, 'W'];
let bermudaTCoordinates: [number, number, string, number, number, string] = [25, 0 , 'N' , 71, 0, 'W'];
These tuple type annotations guarantee that the types of the elements will be valid function parameters for gpsNavigate().
Now, we use JavaScript’s spread syntax to write a very readable function call:
gpsNavigate(...codecademyCoordinates, ...bermudaTCoordinates)
// And by the way, this makes the return trip really convenient to compute too:
gpsNavigate(...bermudaTCoordinates, ...codecademyCoordinates)
// If there is a return trip .
Related articles
10 Essential Steps for Migrating Your JavaScript Project to TypeScript in 2025
Discover the practical roadmap to seamlessly transition your JavaScript projects to TypeScript, enhancing type safety and developer productivity in 2025.
Crafting Dynamic Data-Driven User Interfaces with React and TypeScript
Learn how to effortlessly craft dynamic, data-driven user interfaces using React and TypeScript, enhancing your app's responsiveness and user experience.
Mastering Next-Gen API Security: JWTs with Node.js & TypeScript in 2025
Learn step-by-step how to enhance your API’s security using JWTs with Node.js and TypeScript, preparing your applications for the security demands of 2025.