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.
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.
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.
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.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript