Arrays
7 min · TypeScript
Hold many values under one typed name, and reach for the one you want.
Enemy 1 of 5
This makes a list of three fruit names, typed as string[] so only text is allowed inside. It then prints the list. Press Run to see it.
Why this works
An array holds several values in order, inside [ ], separated by commas: one name, many values, exactly as in JavaScript. TypeScript lets you label what kind of values it should hold: string[] means "an array of strings," number[] means "an array of numbers."
Once an array is typed this way, TypeScript enforces it everywhere the array is used: trying to fruits.push(5) on a string[] would be flagged as an error immediately, since 5 isn't a string. This is one of the clearest everyday wins of typed arrays: a whole class of "I accidentally put the wrong kind of thing in this list" bugs simply can't happen.