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
privateis 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. privateis erased with the types. Before Node runs a TypeScript file, it strips the types out, and the wordprivategoes with them. The field is an ordinary property while the program runs, soJSON.stringify()andconsole.log()show it.- A
#privatefield 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:
privateis a check,#privateis the language. Aprivatefield 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#privatefield. Code inside the class can still use it. protectedis 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:
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:
$ 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:
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:
$ 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.