Sub topic 2
How TypeScript Infers Array Literal Types
TypeScript can infer an array type from the values in an array literal. This chapter assumes that you know basic JavaScript arrays, TypeScript annotations, and union types.
Inferring the element type
When TypeScript sees an array literal, it examines its elements and chooses an element type.
const scores = [90, 85, 72];
// number[]
const names = ["Ada", "Linus"];
// string[]
const values = [1, "one", 2];
// (string | number)[]The type before [] describes the type of every element. An array containing both strings and numbers has a union element type: (string | number)[].
TypeScript usually widens literal values in a mutable array. For example, 90 becomes number, and "Ada" becomes string. This allows later assignments that are compatible with the wider type.
const scores = [90, 85];
scores.push(100); // allowed
scores.push(95.5); // allowedThe variable is declared with const, but the array is still mutable. const prevents assigning a new array to scores; it does not make the elements read-only.
`const` does not create a tuple
A tuple is an array with a fixed length and a specific type at each position. A normal array literal is inferred as an array, even when it is assigned to a const variable.
const point = [10, 20];
// number[]
const record = [1, "Ada"];
// (string | number)[]TypeScript does not infer that record must always have exactly two elements. If the position and length matter, provide a tuple annotation or use as const.
const point: [number, number] = [10, 20];
const record = [1, "Ada"] as const;
// readonly [1, "Ada"]as const keeps literal types and makes the resulting tuple readonly. It is useful for fixed configuration values, command names, and other data that should not change.
Contextual typing
Sometimes the surrounding code gives TypeScript the expected type. This is called contextual typing.
function average(values: number[]): number {
return values.reduce((total, value) => total + value, 0) / values.length;
}
average([10, 20, 30]);The argument [10, 20, 30] is checked as a number[] because average requires that type.
An explicit annotation also provides context:
const ids: number[] = [101, 102, 103];
const labels: Array<string> = ["draft", "published"];Use an annotation when the array's intended type should be clear at its declaration, especially for empty arrays.
const queue: string[] = [];
queue.push("first");Without the annotation, an empty array has no element values from which to infer a useful type. In some situations TypeScript treats it as an evolving array, but its behavior depends on where it is declared and compiler settings. An explicit type avoids that uncertainty.
Index access and compiler settings
For an ordinary array, TypeScript normally assumes that an indexed read produces the element type.
const scores = [90, 85, 72];
const first = scores[0];
// numberJavaScript permits an index that does not exist, however. With the noUncheckedIndexedAccess compiler option enabled, the same read is treated as possibly missing:
const first = scores[0];
// number | undefinedThis setting makes code handle empty arrays and invalid indexes explicitly.
Exercise
Exercise
What types do TypeScript infer for const a = [true, false] and const b = [true, 0]? How would you make b a fixed two-element tuple?
Show answer
a is boolean[], and b is (boolean | number)[]. Write const b: [boolean, number] = [true, 0].