Arrays & Tuples

Arrays & Tuples: A TypeScript array is like a grocery aisle that won't let the product type drift — you can only put fruit on the 'fruit aisle' shelf, never a laptop in the middl

A TypeScript array is like a grocery aisle that won't let the product type drift — you can only put fruit on the 'fruit aisle' shelf, never a laptop in the middle of it. Java already did this with List : a list that only accepts Strings. So if Java already solved this, why does TypeScript's string[] syntax look different/shorter? Because TypeScript writes the type once for the whole array instead of per element (string[] is shorthand for what Java's List expresses), and the generic syntax (Array ) says the exact same thing, just more verbosely. The QA cost is concrete: when a number or null accidentally slips into a test-data array (say, a corrupted record leaking into a user list returned by an API), plain JavaScript with no type checking accepts it silently and only blows up in production with 'undefined is not a function'; a typed array catches that at compile time, before the test even runs.

Micro Lab: TypeScript coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Typed arrays — what differs from Java?

Two syntaxes, same result

string[] and Array are identical — string[] is more common

Wrong type errors immediately

names.push(42) — can't push number into string[], tsc errors

filter/map/reduce are typed

testCases.filter(t => !t.passed) — TypeScript knows return type: TestCase[]

Order the steps to create a typed array:

Determine the element type

Choose string[] or Array syntax

Add only correctly typed elements