Bird
Raised Fist0
Typescriptprogramming~5 mins

Tuple type definition in Typescript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a tuple type in TypeScript?
A tuple type in TypeScript is an array with a fixed number of elements where each element can have a different type. It defines the exact types and order of elements.
Click to reveal answer
beginner
How do you define a tuple type with a string and a number in TypeScript?
You define it like this: <code>let example: [string, number];</code> This means the first element must be a string and the second a number.
Click to reveal answer
intermediate
Can tuple elements be optional in TypeScript? How?
Yes, tuple elements can be optional by adding a question mark after the type. Example: <code>let example: [string, number?];</code> means the second element can be a number or undefined.
Click to reveal answer
beginner
What happens if you try to assign wrong types to a tuple in TypeScript?
TypeScript will show an error because tuples expect specific types in a fixed order. For example, assigning a number where a string is expected will cause a type error.
Click to reveal answer
advanced
How can you use rest elements in tuple types?
You can use rest elements to allow a variable number of elements of the same type at the end. Example: <code>let example: [string, ...number[]];</code> means one string followed by any number of numbers.
Click to reveal answer
How do you declare a tuple with a boolean and a string in TypeScript?
Alet t: [boolean, string];
Blet t: (boolean | string)[];
Clet t: boolean, string;
Dlet t: {boolean, string};
What is true about tuple types in TypeScript?
AThey have fixed length and fixed types per position.
BThey can only contain elements of the same type.
CThey are the same as arrays.
DThey cannot have optional elements.
How do you make the last element of a tuple a variable number of strings?
Alet t: [number, string?];
Blet t: [number, string];
Clet t: [number, ...string[]];
Dlet t: [number, string[]];
What error occurs if you assign [42, 'hello'] to let t: [string, number]?
ASyntax error.
BNo error, assignment is valid.
CRuntime error only.
DType error because the first element should be string, not number.
Can tuple types have optional elements in TypeScript?
AOnly the first element can be optional.
BYes, by adding a question mark after the type.
CNo, tuples must always have all elements.
DOptional elements are only for arrays.
Explain what a tuple type is in TypeScript and how it differs from a regular array.
Think about how many elements and what types each element can have.
You got /4 concepts.
    Describe how to define a tuple with optional and rest elements in TypeScript.
    Consider how to allow some elements to be missing or repeated.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is a tuple type in TypeScript?
      easy
      A. A fixed-length array with specific types for each position
      B. An array that can only contain numbers
      C. A variable-length array with any types
      D. A function that returns multiple values

      Solution

      1. Step 1: Understand tuple definition

        A tuple is an array with fixed length and specific types at each position.
      2. Step 2: Compare options

        Only 'A fixed-length array with specific types for each position' matches the tuple definition.
      3. Final Answer:

        A fixed-length array with specific types for each position -> Option A
      4. Quick Check:

        Tuple = fixed-length, typed array [OK]
      Hint: Tuples fix length and types in order [OK]
      Common Mistakes:
      • Thinking tuples are just any arrays
      • Confusing tuples with functions
      • Assuming tuples can change length
      2. Which of the following is the correct syntax to define a tuple type with a string and a number in TypeScript?
      easy
      A. let data: string | number;
      B. let data: (string, number);
      C. let data: {string, number};
      D. let data: [string, number];

      Solution

      1. Step 1: Recall tuple syntax

        Tuples use square brackets with types separated by commas, e.g., [string, number].
      2. Step 2: Check options

        Only let data: [string, number]; uses square brackets correctly. Options B and C use wrong symbols, D is a union type.
      3. Final Answer:

        let data: [string, number]; -> Option D
      4. Quick Check:

        Tuple syntax = square brackets [OK]
      Hint: Use square brackets for tuple types [OK]
      Common Mistakes:
      • Using parentheses instead of brackets
      • Using curly braces which define objects
      • Confusing union types with tuples
      3. What will be the output of this TypeScript code?
      let tuple: [string, number] = ['age', 30];
      console.log(tuple[0]);
      console.log(tuple[1]);
      medium
      A. undefined and undefined
      B. 30 and "age"
      C. "age" and 30
      D. Error: Type mismatch

      Solution

      1. Step 1: Understand tuple assignment

        The tuple is assigned ['age', 30], so index 0 is 'age' (string), index 1 is 30 (number).
      2. Step 2: Check console.log outputs

        console.log(tuple[0]) prints 'age', console.log(tuple[1]) prints 30.
      3. Final Answer:

        "age" and 30 -> Option C
      4. Quick Check:

        Tuple values printed in order [OK]
      Hint: Tuple indexes match type order [OK]
      Common Mistakes:
      • Swapping tuple elements order
      • Expecting errors on correct assignment
      • Confusing tuple with object properties
      4. Identify the error in this TypeScript tuple declaration:
      let info: [string, number] = [25, 'John'];
      medium
      A. The tuple elements are in the wrong order
      B. Tuple length is incorrect
      C. Missing type annotation
      D. No error, code is correct

      Solution

      1. Step 1: Check tuple type order

        The tuple type expects [string, number], so first element must be string, second number.
      2. Step 2: Compare assigned values

        The assigned array is [25, 'John'], which is [number, string], reversed order causing type error.
      3. Final Answer:

        The tuple elements are in the wrong order -> Option A
      4. Quick Check:

        Tuple type order must match values [OK]
      Hint: Match tuple types order exactly [OK]
      Common Mistakes:
      • Swapping values ignoring type order
      • Assuming tuple length mismatch
      • Forgetting to add type annotation
      5. You want to define a tuple type to store a person's name (string), age (number), and a boolean indicating if they are a student. Which is the correct tuple type and example value?
      hard
      A. let person: [number, string, boolean] = [25, 'Alice', true];
      B. let person: [string, number, boolean] = ['Alice', 25, true];
      C. let person: [string, boolean, number] = ['Alice', 25, true];
      D. let person: [string, number] = ['Alice', true, 25];

      Solution

      1. Step 1: Define tuple type order

        The tuple should be [string, number, boolean] for name, age, and student status respectively.
      2. Step 2: Check example values

        let person: [string, number, boolean] = ['Alice', 25, true]; matches the type order and values: 'Alice' (string), 25 (number), true (boolean).
      3. Final Answer:

        let person: [string, number, boolean] = ['Alice', 25, true]; -> Option B
      4. Quick Check:

        Tuple type matches value order and types [OK]
      Hint: Order types as data appears in tuple [OK]
      Common Mistakes:
      • Mixing up type order
      • Using wrong types for positions
      • Incorrect tuple length
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard