How to fix Tuple type 'X' of length 2 has no element at index 3 in TypeScript
TypeScript tuples are fixed-length arrays and each index must be explicitly declared. Accessing an index that is outside the known length causes the compiler to throw the "Tuple type 'X' of length 2 has no element at index 3" error.
Quick fix
Fixes Tuple type 'X' of length 2 has no element at index 3
const coords: [number, number] = [10, 20];
console.log(coords[3]); // ❌ Tuple type '[number, number]' of length 2 has no element at index 3- Inspect the tuple declaration and the failing index
- Keep your access within the declared bounds
- Extend the tuple when you legitimately need another slot
- Switch to an array or a rest-enabled tuple for variable sizes
const coords: [number, number] = [10, 20];console.log(coords[3]); // ❌ Tuple type '[number, number]' of length 2 has no element at index 3Tuple type 'X' of length 2 has no element at index 3
What this error means
When TypeScript knows a variable is a tuple of length N, every index that you read or write must be in the range 0..N-1. Mixing constant indexes that are outside that range tells the compiler you are trying to read a position that cannot exist, so it fails before emitting JavaScript. The error message uses a placeholder (X) for the tuple type and reports the invalid index so you can correct the type or the value you are trying to access.
Typical symptoms
- `tsc` or your IDE reports "Tuple type "X" of length 2 has no element at index 3" during compilation or type checking
- Red squiggly lines or lint warnings on the tuple access expression
- A build or watch task stops because the tuple read is treated as a hard type violation
- IntelliSense suggest only the first two indexes even though your code tries to read the third element
Common causes
- Trying to read `tuple[3]` from a tuple declared as `[string, number]` or another two-element type
- Using `as const` or literal tuples and then indexing with a hard-coded number greater than the tuple length
- Mixing a tuple definition and a dynamic index that exceeds the known element count
- Treating tuples as a regular array without updating the type to reflect its required entries
How to fix "Tuple type 'X' of length 2 has no element at index 3"
Look at the tuple definition near the error. If the type is declared as [string, number], the available indexes are only 0 and 1. The compiler shows the invalid index (3 in this message) so you know the tuple is too short for that access. Example:
const coords: [number, number] = [10, 20];
console.log(coords[3]); // ❌ Tuple type '[number, number]' of length 2 has no element at index 3Find the tuple and index that triggered the message and confirm the size before proceeding.
Limit reads to indexes that exist. Use the first two positions or destructure the tuple so TypeScript can infer the values without an extra index:
const [x, y] = coords;
console.log(x, y); // ✅ uses indexes 0 and 1Avoid writing coords[2] unless you have at least three entries in the type.
If your data now carries a third value (for example a label, flag, or optional metadata), add it to the tuple type and value:
const coords: [number, number, number] = [10, 20, 30];
console.log(coords[2]); // ✅ now index 2 existsUpdating the type keeps the runtime value in sync with what TypeScript expects.
When the number of entries can grow, prefer a regular array or a tuple with a rest element so the compiler knows you allow more values. For example:
type Measurements = [number, number, ...number[]];
const values: Measurements = [10, 20, 30, 40];
console.log(values[3]); // ✅ rest captures additional elementsThis tells TypeScript the tuple starts with two fixed entries and can have more, so indexing past the first two is valid.
If you receive an index from user input or another function, check the tuple length before indexing:
function readAt<T extends unknown[]>(tuple: T, index: number) {
if (index < tuple.length) {
return tuple[index];
}
throw new Error('Index out of bounds');
}Using tuple.length keeps the compiler happy while you safely handle long indexes in a single place.
Advanced notes
Tuple types are compared to their exact lengths, so [string, number] is incompatible with [string, number, boolean] even if the runtime values look similar. When you need more flexibility, prefer readonly [A, ...B[]] or Array<A> and then validate the contents, because tuples are best used for fixed schemas such as (name, age) pairs. TypeScript will keep enforcing the index range even if the tuple comes from as const or a tuple-returning API, which helps you catch off-by-one bugs early.
Frequently asked questions
- What does "Tuple type 'X' of length 2 has no element at index 3" mean?
- N-1. Mixing constant indexes that are outside that range tells the compiler you are trying to read a position that cannot exist, so it fails before emitting JavaScript. The error message uses a placeholder (X) for the tuple type and reports the invalid index so you can correct the type or the value you are trying to access.
- How do I fix "Tuple type 'X' of length 2 has no element at index 3"?
- TypeScript tuples are fixed-length arrays and each index must be explicitly declared. Accessing an index that is outside the known length causes the compiler to throw the "Tuple type 'X' of length 2 has no element at index 3" error.
- What causes "Tuple type 'X' of length 2 has no element at index 3"?
- Trying to read tuple[3] from a tuple declared as [string, number] or another two-element type Using as const or literal tuples and then indexing with a hard-coded number greater than the tuple length Mixing a tuple definition and a dynamic index that exceeds the known element count
- Is the "Tuple type 'X' of length 2 has no element at index 3" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚TypeScript Official Documentation
- 📖TypeScript Handbook - Basic Types: Tuple(Official Docs)
- 📖TypeScript Handbook - Object Types: Tuple(Official Docs)