An array type is the element type followed by [], as in {string[]}, or the generic form {Array<string>}; the two are the same type. TypeScript checks every element you add and types every element you read.
For an array of objects, put the object type before the brackets, {{ sku: string, quantity: number }[]}, or name the object with @typedef and write {LineItem[]}.
At a glance
| Use | JSDoc in a .js file | TypeScript in a .ts file |
|---|---|---|
| Array of strings | /** @type {string[]} */ | : string[] |
| Generic spelling | /** @type {Array<string>} */ | : Array<string> |
| Array of objects | /** @type {{ sku: string, quantity: number }[]} */ | : { sku: string; quantity: number }[] |
| Closure spelling | /** @type {Array.<string>} */ | : Array<string> |
Examples
/** @type {string[]} */
const sizes = ["S", "M", "L"];
/** @type {Array<number>} */
const prices = [19.5, 12];
/** @type {{ sku: string, quantity: number }[]} */
const lines = [{ sku: "TEE-01", quantity: 2 }];
/** @type {Array<{ sku: string, quantity: number }>} */
const saved = [];
sizes.push("XL");
saved.push(...lines);
prices.map((p) => p.toFixed(2));
export { sizes, prices, lines, saved };const sizes: string[] = ["S", "M", "L"];
const prices: Array<number> = [19.5, 12];
const lines: { sku: string; quantity: number }[] = [{ sku: "TEE-01", quantity: 2 }];
const saved: Array<{ sku: string; quantity: number }> = [];
sizes.push("XL");
saved.push(...lines);
prices.map((p) => p.toFixed(2));
export { sizes, prices, lines, saved };jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": []
},
"include": ["*.js"]
} /** @type {string[]} */
const sizes = ["S", "M", "L"];
sizes.push(42);error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
export { sizes };jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": []
},
"include": ["*.js"]
} // In a .js file an empty array literal starts as any[], so nothing below is checked.
const ids = [];
ids.push(100);
ids.push("A-101");
/** @type {number[]} */
const orderNumbers = [];
orderNumbers.push(100);
orderNumbers.push("A-101");error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
export { ids, orderNumbers };jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": []
},
"include": ["*.js"]
} Empty arrays need a type
In a .js file, const ids = [] starts as any[], so anything can be pushed into it, and its element type becomes whatever was pushed, as the third example shows. Give empty arrays a type at the declaration: /** @type {number[]} */ const ids = [];.
An array literal with elements is typed from them: ["S", "M"] is string[].
Arrays of unions
Parentheses matter with unions. {(string | number)[]} is an array whose elements are strings or numbers; {string | number[]} is either one string or an array of numbers.
Gotchas
- Reading
items[5]is typed as the element type even when the array is shorter. ThenoUncheckedIndexedAccessoption addsundefinedto such reads. - For an array that should not change, use
readonly string[]; for a fixed number of elements, use a tuple.
Questions
How do I write an array of objects in JSDoc?
@type {{ name: string, price: number }[]}, or define the object with @typedef and use @type {Product[]}. Array<Product> works too.
Is Array<string> the same as string[]?
Yes. They are two spellings of one type, and the older Array.<string> is a third.
Sources
- Everyday Types: arrays · TypeScript handbook
- JSDoc Reference: types · TypeScript handbook
- Type Checking JavaScript Files: empty array initializers · TypeScript handbook
Last reviewed 20 October 2026.