Loading video player…

Keeping Fields Private at Run Time

In this video, you saw a volunteer mark the coach’s phone number in the league’s Team class as private, watched it leak into the JSON for the league’s website anyway, and kept it hidden with a JavaScript #private field instead.

Key Points

  • TypeScript’s private is a check. The checker stops code outside the class from reading the field with a dot, and reports error TS2341 when it tries. Node doesn’t check types, so it still runs a file with that error.
  • The checker doesn’t stop everything. A file that never names the field passes the check, and so does a read with square brackets, such as lions["coachPhone"]. The checker lets that read through on purpose, as an escape hatch.
  • private is erased with the types. Before Node runs a TypeScript file, it strips the types out, and the word private goes with them. The field is an ordinary property while the program runs, so JSON.stringify() and console.log() show it.
  • A #private field is part of JavaScript. It stays when the types are stripped, and the language keeps it hidden, so it isn’t in the JSON or the logged object. Reading it from outside the class is an error for the checker, and Node won’t load the file at all.
  • The rule: private is a check, #private is the language. A private field is enough for a detail other scripts shouldn’t build on but may see, like when a team’s details last changed. For anything that mustn’t leak, use a #private field. Code inside the class can still use it.
  • protected is the same kind of check, and it also lets a class that extends this one use the field. It’s gone at run time too.

The Code

The Team class with its coach’s phone number marked private, and a script that posts the team as JSON:

Language: TypeScript Filename: team.ts
class Team {
  name: string;
  ageGroup: string;
  private coachPhone: string;

  constructor(name: string, ageGroup: string, coachPhone: string) {
    this.name = name;
    this.ageGroup = ageGroup;
    this.coachPhone = coachPhone;
  }
}

const lions = new Team("Riverside Lions", "U10", "555-0142");
console.log(JSON.stringify(lions));

The checker reports nothing for this file, and the phone number still ends up in the JSON:

Language: Shell
$ node team.ts
{"name":"Riverside Lions","ageGroup":"U10","coachPhone":"555-0142"}

The same class with a #coachPhone field, in its declaration and in the constructor, and the team logged as well:

Language: TypeScript Filename: team.ts
class Team {
  name: string;
  ageGroup: string;
  #coachPhone: string;

  constructor(name: string, ageGroup: string, coachPhone: string) {
    this.name = name;
    this.ageGroup = ageGroup;
    this.#coachPhone = coachPhone;
  }
}

const lions = new Team("Riverside Lions", "U10", "555-0142");
console.log(JSON.stringify(lions));
console.log(lions);

This time, neither the JSON nor the logged team shows the phone number:

Language: Shell
$ node team.ts
{"name":"Riverside Lions","ageGroup":"U10"}
Team { name: 'Riverside Lions', ageGroup: 'U10' }

Next, in Exercise: Type the Team Roster, you’ll type a class for a team’s players and keep their guardians’ contact details out of the roster’s JSON. It’s the course’s first coding exercise, and it runs right in your browser.

00:00 You’ve typed the results table. The league’s website also lists every team, from a small Team class. A volunteer script posts that list as JSON.

00:10 That’s the class where the volunteer adds each coach’s phone number, for match-day calls. That number is only for volunteers, and the website mustn’t show it.

00:19 So the volunteer marked that field private, to keep it inside the class. In this video, you’ll find out what private really protects, and what JavaScript itself can keep hidden.

00:31 Watch for now. You’ll write a class like this in the exercise.

00:37 Here’s the league’s Team class. The keyword private sits in front of the coach’s phone field. Inside the class, the constructor sets it like any other field.

00:47 Now say a script reads the Lions’ phone number from outside the class. You run npm run check, and the checker stops at that line. The property is private, and only code inside the Team class may use it.

01:02 Node doesn’t check types, so it would still run this file. But to the volunteer, a file the checker rejects never ships, so the number looks safe.

01:13 The website script never reads the phone number. It turns the Lions into JSON with JSON.stringify, and prints the result. This time, the checker reports nothing.

01:26 Your turn: pause the video and predict what Node prints when it runs this file.

01:36 The coach’s phone number is right there in the JSON, next to the team’s name and age group. Logging the team with console.log shows the phone number too.

01:48 The checker has a gap of its own. Write the field’s name in quotes inside square brackets, and it lets the read through on purpose, as an escape hatch. Node prints the number again.

02:01 Why does a private field leak? Look at what Node runs. Before Node runs a TypeScript file, it strips the types out. Each annotation goes, and the word private goes with them.

02:14 What’s left is a plain JavaScript class with three ordinary fields. JSON.stringify and console.log see all three, because nothing marks the phone number as different.

02:26 So private is a check. Before the program runs, it stops other TypeScript code from reading the field with a dot. While the program runs, the field is a normal property.

02:38 JavaScript has its own private fields. Only code inside the class can reach them, and their names start with a hash sign. Write the field as #coachPhone, in its declaration and in the constructor.

02:52 That’s the only change. Strip the types again, and the hash stays. Stripping removes only types, and the hash is part of the JavaScript. Run the file, and the JSON has only the name and the age group.

03:08 The logged team has no phone number either.

03:11 Now try reading the field from outside the class. The checker reports it, and Node refuses to load the file at all. That’s a syntax error from JavaScript itself.

03:22 Code inside the class can still use it, so a method could hand the number out on purpose, for match-day calls.

03:30 So you have two kinds of privacy. Private is a check, and the hash is the language.

03:37 You’ll meet private often in TypeScript code. It’s enough for a detail other scripts shouldn’t build on, like when a team’s details last changed. Nothing breaks if that shows up in the JSON.

03:48 For anything that mustn’t leak, like a coach’s phone number, use a hash field. You may also see protected in TypeScript code. It’s the same kind of check, and it also lets a class that extends this one use the field.

04:02 It’s gone at run time too. Next, in Exercise: Type the Team Roster, you’ll type a class for a team’s players, and keep their guardians’ contact details out of the roster’s JSON.

04:15 It’s the course’s first coding exercise, and it runs in your browser.

Become a Member to join the conversation.