Loading video player…

Checking Code Before It Runs

In this video, you saw the two separate moments in a TypeScript program’s life, using a small piece of Spoke & Sprocket’s repair-ticket code.

Key Points

  • Before the program runs, TypeScript’s checker reads your code and its types. It reports where a value doesn’t fit, and it runs nothing. When everything fits, it reports nothing.
  • A type annotation, such as : number after a parameter, says which kind of value belongs in that spot. You’ll write your own in a later lesson.
  • To run the program, Node erases the types first. What’s left is plain JavaScript, and that’s what Node runs. Your file keeps its types. Node drops them only as it loads the file.
  • Types aren’t checked while your program runs. A value that arrives at run time, such as text read from a saved file with JSON.parse(), isn’t checked against an annotation. Data from outside has to be checked by code that runs, which a later lesson shows.

The Code

The shop’s ticket function, with its annotations:

Language: TypeScript Filename: tickets.ts
function nextTicket(last: number): number {
  return last + 1;
}

console.log(nextTicket(41));

The checker reports nothing for this file. Once the types are erased, the JavaScript that’s left prints the next ticket number:

Language: Program Output
42

The same file, tickets.ts, with its last line replaced by two lines that read the last ticket number from the shop’s saved file, where it’s stored as text:

Language: TypeScript Filename: tickets.ts
function nextTicket(last: number): number {
  return last + 1;
}

const saved = JSON.parse('{"lastTicket": "41"}');
console.log(nextTicket(saved.lastTicket));

The checker reports nothing here either. JSON.parse() can give back any shape, so the checker doesn’t know what’s in saved and doesn’t check saved.lastTicket against number.

At run time, saved.lastTicket is the string "41", and a string plus a number joins the two:

Language: Program Output
411

Next, in Checking and Running a Project, you’ll set up both jobs on your own machine, and the checker makes its first real find in the shop’s code.

00:00 Some of Dee’s quotes at Spoke and Sprocket have come out wrong. Nobody noticed until a customer was already holding one. With JavaScript, a wrong value only shows up while the program runs, if anyone notices at all.

00:14 In this video, you’ll see a second moment, before the program runs, and the job TypeScript does in it. The code behind those quotes comes later. First, a smaller piece of the shop’s code, in a TypeScript file that numbers its repair tickets.

00:30 The function gives the next ticket number after the last one. It’s JavaScript you can already read, with one addition. Each colon followed by number is a type annotation.

00:42 It says which kind of value belongs in that spot. One is on the parameter, and one is on what the function returns. Writing your own annotations comes in a later lesson.

00:53 For now, you only need to read them.

00:57 Before anything runs, TypeScript’s checker reads the file. The call passes 41. The code itself shows that’s a number, so it fits the parameter. Adding 1 to a number gives a number, so the result fits what the function promises to return.

01:16 Everything fits, so the checker reports nothing. It read every line without running any of them. If the call passed the string 41, in quotes, it wouldn’t fit.

01:28 The checker would tell you where, before you run anything.

01:33 Now the second moment. When Node runs the file, it erases the types first. Each annotation disappears, and what’s left is plain JavaScript, the same function you’d have written before you met any types.

01:47 Node runs that JavaScript, and it prints 42, the next ticket number. So the types are there while the checker reads, and gone by the time the program runs.

01:58 Your file keeps them. Node drops them only as it loads the file.

02:04 Now take a value from outside your code. The shop keeps its last ticket number in a saved file, and the file stores it as text. To keep this example small, the file’s text sits right in the code.

02:17 JSON.parse turns that text into an object while the program runs. Its last ticket is the string 41, in quotes. The checker reads this version too, and reports nothing.

02:31 Earlier, it could tell a quoted 41 didn’t fit. But JSON.parse can give back any shape at all, so the checker can’t tell what the last ticket holds. It lets that value into the number spot without a check.

02:45 Then the types are erased, and the function that’s left adds 1 to whatever it gets. Your turn: pause the video and predict what this program prints.

03:00 It prints 411.

03:04 A string plus a number joins the two, as JavaScript always does. Dee’s next ticket would be 411.

03:12 The annotation said number, but nothing checked it as the program ran. Watch out for that: types are never checked while your program runs.

03:21 Data from outside has to be checked by code that runs. A later lesson shows how, and that’s all you need about it for now.

03:31 So TypeScript works in two separate times. Before the program runs, the checker reads the types and reports what doesn’t fit. While it runs, the types are gone, and plain JavaScript does exactly what it always did.

03:46 Next, in Checking and Running a Project, you’ll set up both jobs on your own machine, and the checker makes its first real find in the shop’s code.

Become a Member to join the conversation.