Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Letting Inference Do the Work

In this video, you followed the types through Spoke & Sprocket’s parts total for a brake job and saw how much the checker works out with only one annotation in the file. Every annotation is one more thing to update when the code changes, so you write only the ones the checker needs.

Key Points

  • Type inference is the checker working out a type from a value. const brakeJob = [1899, 799]; has no annotation, but its value is an array of numbers, so its type is number[].
  • Hover to confirm. Hover over a name in your editor, and the tooltip shows the type the checker inferred, such as const brakeJob: number[].
  • Types flow from one name to the next. Because prices is a number[], .reduce() passes numbers to its callback, so sum and price are numbers. Their types come from where the callback is passed. .reduce() on an array of numbers, starting from 0, gives back a number, so the checker infers the return type of partsTotal() as number, and total, set from that result, is a number too.
  • A callback’s return type shapes the result. The callback that brakeJob.map() gets on the labels line returns a string, so .map() builds a string[] for labels.
  • Watch out for let with a single value: a const holding a single string or number keeps that exact value as its type (const size = "700x28c" gives "700x28c"), while a let widens it (to string), because a let can change later. So a let can’t go where only certain exact values are allowed.
  • Write the type yourself in three places:
  • A function’s own parameters. A parameter’s value arrives only when someone calls the function. Without : number[] on prices, the check reports an implicit any for prices and for both callback parameters. A callback’s parameters are different: they get their types from where it’s passed.
  • Exported functions’ returns. Inference finds number here too, but a written return type is a promise to the files that import the function. Without it, a change like .toFixed(0) quietly changes the inferred type, and the error turns up in quote.ts. With it, the error shows up in parts.ts, where the change is.
  • Arrays and variables that start empty. An empty array has no values yet, so annotate it with what it’s meant to hold, like const reorderList: (string | number)[] = [];. The same goes for a variable declared without a value, like the let rate: number; you wrote in Annotating Functions and Variables, where the rate got its value later in one of two branches.
  • Everywhere else, let inference do the rest. When a value already gives the type you mean, an annotation only repeats what the checker knows. An annotation that says more than the value, like the [string, number] tuple for a booking, stays.

The Code

With one annotation, the checker infers every other type in the file:

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

00:00 In the last exercise, you traced an error back to where it started. So far, you’ve annotated parameters, returns and arrays. Back in the labor cost, though, cents needed none.

00:11 Every annotation is one more thing to update when Dee’s code changes. So which ones does the checker actually need? Here’s the parts total from the arrays lesson, with plain prices in cents.

00:24 Only one name has an annotation, the prices parameter. Look at what the checker knows about the rest.

00:31 Start with brakeJob, on line 1. It has no annotation. However, it has a value, an array of two numbers. The checker reads that value and gives brakeJob its type.

00:44 You can confirm this in your editor: hover over brakeJob, and the tooltip says it’s an array of numbers. Working out a type from a value like this is called type inference.

00:55 Watch one detail here. A const holding a single string or number keeps that exact value as its type, while a let widens it, because a let can change later.

01:05 So a let can’t go where only certain exact values are allowed. That’s all you need about exact-value types for now.

01:15 Inference doesn’t stop at one line. You saw in the arrays lesson that reduce() on the prices array passes numbers to the callback. That’s where sum and price get their types.

01:26 Hover over price, and the tooltip confirms it’s a number. It has no annotation of its own. Its type comes from where the callback is passed. reduce() on an array of numbers, starting from zero, gives back a number, so the function returns a number.

01:43 The checker infers that return type too. And total, set from that result, is a number as well. Every question mark is gone, and the file still has only one annotation.

01:56 Now add a line that labels each part for the quote. The map() method calls the callback on each price, and the callback returns a template literal with the price and the word cents.

02:07 Your turn. Pause the video and predict the type the checker infers for labels, before you hover.

02:19 It’s an array of strings.

02:22 Each call returns a string, so the map() method builds an array of strings.

02:28 Run the check, and it’s clean. Run the file, and Node prints the two labels and the total, 2698.

02:38 Inference works from values. Three places still call for an annotation. A function’s own parameter is the first. Its value arrives only when someone calls the function.

02:50 Without the annotation on prices, you’ve seen the check report an implicit any for prices and for both callback parameters. A callback’s parameters are different: they get their types from where it’s passed.

03:03 An exported function’s return is the second. Say the quote script imports partsTotal() to work out dollars. Inference finds number here too. However, without the annotation, a change like the toFixed() one in the labor cost quietly changes the inferred type, and the error turns up in the quote script, far from the change.

03:23 With the return type written, the error shows up here, where the change is. An empty array is the third. It has no values yet, so annotate it with what it’s meant to hold, like the tube reorder list.

03:39 Everywhere else, when a value already gives the type you mean, an annotation only repeats what the checker knows. An annotation that says more than the value, like the tuple type for a booking, stays.

03:52 So annotate a function’s own parameters, exported returns and arrays that start empty. Let inference do the rest, and hover when you want to see what it found.

04:03 Next, in Exercise: Trim the Annotations, you’ll go through a helper a teammate’s AI assistant wrote, and decide which of its annotations the checker needs.

Become a Member to join the conversation.