Typing Arrays and Tuples

Your settings loader no longer lets any in, so the checker follows every value it reads. The shop’s lists are a different story. Dee’s code keeps the parts on a job, the inner tubes to reorder, and the bookings for the workshop board in arrays, and nothing tells the checker yet what may go in them.

In this lesson, you’ll write types for those lists. You’ll describe an array whose items share a type, start an empty list that the checker can still follow, and use a tuple when each position in a short array means something different. Start with the list the shop uses most, the prices of the parts on a job.

Type a List of Prices

A function that takes an array has the same gap that laborCost() had. From the parameter alone, the checker can’t tell what the array holds. Spoke & Sprocket adds up a job’s parts before it adds labor, and every price on a job is a number of cents. An array whose items are all numbers has the type number[], the type of every item followed by empty square brackets.

Create parts.ts in your repair-queue project folder with the shop’s parts total, and annotate its parameter with that type:

Language: TypeScript Filename: parts.ts
 1const brakeJob = [1899, 799];
 2const chainJob = [2499, 799];
 3
 4function partsTotal(prices: number[]) {
 5  return prices.reduce((sum, price) => sum + price, 0);
 6}
 7
 8console.log("Brake job parts:", partsTotal(brakeJob));
 9console.log("Chain job parts:", partsTotal(chainJob));

Check and run the file:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

$ node parts.ts
Brake job parts: 2698
Chain job parts: 3298

The annotation on line 4 covers more than prices. Without it, the check reports three errors: prices and the callback parameters sum and price are each implicitly any. With prices typed as number[], the checker knows that .reduce() passes numbers to the callback, so the code inside the function is checked too.

And if a price copied from a form as text, such as "799", ever lands in chainJob, the check stops the call with Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'.

Notice that brakeJob and chainJob have no annotations. They start with values, so the checker already knows their type. In other people’s code, you’ll also see number[] written as Array<number>. The two spellings mean the same thing, and this course uses number[].

Start a List Empty

An array that starts empty gives the checker no values to infer a type from. The shop keeps a reorder list for inner tubes: whenever the last tube of a size goes, someone adds the size, and Dee orders them all at closing time. Tube sizes come in two forms. Mountain bike tubes go by wheel size in inches, such as 29, and road bike tubes by a label, such as "700x28c". So a size is a number or a string.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.