Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
TypeScript Intermediate FREE

Tuples

Lesson 2 of 17 Intermediate Interactive

Tuples are fixed-length arrays with specific types at each position.

Syntax

TYPESCRIPT
let person: [string, number] = ["John", 30];
let record: [number, string, boolean] = [1, "active", true];
Tuple Types
TYPESCRIPT
let person: [string, number] = ["Alice", 25];
console.log(person[0], person[1]);

function useState(initial: number): [number, (n: number) => void] {
    let value = initial;
    const setter = (n: number) => { value = n; };
    return [value, setter];
}

const [count, setCount] = useState(0);
console.log(count);

Practice

1
Exercise

Practice this concept.

Answer
Write the code as shown above.

Quick Quiz

1

What did you learn?

This covers the basics.

Interview Questions

It is a fundamental TypeScript feature.