ProgramGuru

Tuple Programs

Tuple Programs — purpose and prerequisites

Tuples describe positions with known types and lengths. They are useful for coordinates, results, and fixed small records, though named object properties are often clearer for growing APIs. readonly tuple annotations restrict mutation at compile time.

What you’ll learn

  • Explain the purpose of tuple programs.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The labeled tuple describes two numeric positions. Destructuring gives x and y, and Math.hypot computes sqrt(x² + y²).

Tuple labels improve editor readability but do not create runtime object keys named x or y.

Visualize the flow

Tuple Programs — learning flowFixed tuple positions → Destructuring → Calculated resultTuple Programs1Fixed tuple positions2Destructuring3Calculated resultRun the example and test a boundary case.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

type Point = readonly [x: number, y: number];
function distanceFromOrigin([x, y]: Point): number { return Math.hypot(x, y); }
const location: Point = [3, 4];
console.log(distanceFromOrigin(location));
console.log(location.join(","));

Verified output

5
3,4

Compile: npx tsc --strict --target es2022 --module commonjs tuple-programs.ts; run: node tuple-programs.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The labeled tuple describes two numeric positions. Destructuring gives x and y, and Math.hypot computes sqrt(x² + y²).

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Tuple labels improve editor readability but do not create runtime object keys named x or y.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Create a readonly [latitude, longitude] tuple and write a formatter with fixed decimal places.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes tuple programs in the example?
Choose one answer. You can try again.

Answer and explanation

The labeled tuple describes two numeric positions.

The labeled tuple describes two numeric positions. Destructuring gives x and y, and Math.hypot computes sqrt(x² + y²).

Key idea to remember

The labeled tuple describes two numeric positions. Destructuring gives x and y, and Math.hypot computes sqrt(x² + y²).

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Object Programs →