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

Choosing unknown Over any

In this video, you followed one saved price at Spoke & Sprocket from JSON.parse() all the way to a quote total, first as any and then as unknown.

Key Points

  • any switches the checker off for a value. Whatever you do with it goes through unchecked.
  • any doesn’t always stay where it starts. In this example, a function that returns it with no return type, a variable that holds it, and a total added up from it all become any too, so the missing checks spread all the way to the result.
  • any gets in three common ways. A function can hand it back, as JSON.parse() does. You can write it yourself, as : any. A parameter with no type is an implicit any, which strict mode reports. The first two get in silently, so they’re the ones to look for. Follow an any back to where it first enters, and fix it there.
  • unknown accepts every value too, but it blocks anything that needs a particular type, like adding to it, until you’ve checked what it is. You can still store it, log it or check it with ===. Because a return type is a promise about what comes out, : unknown on savedPrice() stops the any inside the function. The checker reports 'tube' is of type 'unknown'. where the code adds to tube without a check. You can also write : unknown on the variable that holds the parsed value, as in const tube: unknown = JSON.parse(text);.
  • One typeof check makes an unknown value usable. Inside if (typeof tube === "number") { ... }, the checker reads the check and gives tube the narrower type number. That’s called narrowing, and that’s all you need about it for now. The same check works with "string", for a value that should be text.
  • The check works at run time because typeof is plain JavaScript. The unknown annotation is erased before Node runs the file, but the typeof check stays and runs.
  • The rule: for a value whose type you don’t know yet, choose unknown over any, and check its type before you use it as one.

The Code

The first version reads the inner tube’s saved price with JSON.parse(). The saved text is "799", with quotes, so the value is 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

00:00 In the last exercise, the checker made sure every value in your labor estimate was a number. Some values, though, come from outside your code, where the checker can’t see them.

00:11 The shop saves each part’s price from a form. This function reads one saved price, using JSON.parse to turn the saved text into a value. To keep the example small, the saved text sits right in the code.

00:25 The quote adds the inner tube’s price to 3375 cents of labor, the brake service you priced earlier.

00:34 Run the check, and it reports nothing.

00:38 Run the program, and it prints almost eight million cents, nearly eighty thousand dollars. The saved price was the string 799, in quotes, so the plus sign joined the two, the same join as Dee’s 411 ticket.

00:54 Why didn’t the checker catch it? JSON.parse hands back a value whose type is any, as far as the checker knows. Any switches the checker off for that value, so whatever you do with it goes unchecked.

01:08 So the function returns any as well. The tube variable holds that result, so it’s any too. And the total, worked out from tube, gets the same type. You might think any stays where it starts.

01:24 Here, the return, the variable and the sum all pick it up, so the missing checks spread to the quote total.

01:33 Any gets into your code in three common ways. A function can hand it back, the way JSON.parse does. You can write it yourself: a colon and the word any after a name.

01:47 A parameter with no type would be an implicit any, but strict mode reports that one, as you saw with the labor function. The first two get in silently, so look for those.

01:59 Follow an any back to where it enters, and fix it there.

02:04 Give the function unknown as its return type. A return type is a promise, so the any stops inside the function, and the code outside gets unknown. Like any, unknown accepts every value.

02:18 Unlike any, it blocks anything that needs a particular type, like adding to it, until you’ve checked what it is. Run the check again, and this time it reports an error.

02:31 It’s on line 6, and it says tube is of type unknown, so the checker won’t add 3375 to it. That’s the line where the string slipped through before.

02:44 To use an unknown value, check its type with code that runs. Wrap the total in an if statement. Its test checks that typeof tube is number, written as a string in quotes.

02:56 Inside that block, the checker knows tube is a number, so adding 3375 is allowed. The else branch handles every other value. Now the check passes. Run the program, and Node prints that the saved price, the string 799, isn’t a number.

03:14 The string never reaches the total. A clear message beats a wrong quote. Turning the text into a number comes later.

03:24 Save the number 799 instead, and the program prints a quote total of 4174.

03:32 The check works because typeof is plain JavaScript. The annotation is erased before Node runs the file, but the typeof check stays and runs. The same check works with string, for a value that should be text.

03:47 The checker reads the check and gives tube the narrower type number inside the block. That’s called narrowing, and that’s all you need about narrowing for now.

03:57 Any accepts every value and checks nothing, and it can spread to what you work out from it. Unknown accepts every value too, but you check its type before you use it as one.

04:09 For a value whose type you don’t know yet, unknown is the one to choose.

04:14 Next, in Exercise: Find Where any Gets In, you’ll go through a helper an AI assistant suggested for loading the shop’s saved hourly rate.

Become a Member to join the conversation.