Modifiers
Public by default
Members are public unless marked otherwise. Public fields and methods form the normal way to use an object.
class User { public name: string = "Guest"; } const user = new User(); console.log(user.name);
Guest
private walls
Mark internals as private to hide them from outside code. Only methods of the same class can read or change a private member.
Outside access is a compile error, which keeps refactoring safe.
class Vault { private code: string = "1234"; unlock(attempt: string): boolean { return attempt === this.code; } } const vault = new Vault(); console.log(vault.unlock("1234"));
true
Protected for children
Protected members are hidden from outside code but visible inside child classes. Use protected for helpers that children need and strangers do not.
class Animal { protected legs: number = 4; } class Dog extends Animal { describe(): string { return "Legs: " + this.legs; } } console.log(new Dog().describe());
Legs: 4
Readonly fields
Mark a field as readonly when it is set once and never changed after. The compiler rejects later assignments to that field.
class Config { readonly host: string = "localhost"; } const config = new Config(); console.log(config.host);
localhost
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the access of a member with no modifier?
Answer
Public: any code that holds the object can use it
Members are public unless a modifier says otherwise, and
publicmostly documents that choice. -
Who may read or change a
privatemember?Answer
Only methods of the same class
The private wall stops outside access at compile time, which keeps internals free to change.
-
Who may use a
protectedmember?Answer
The class and the classes that extend it
Protected keeps the member away from strangers while child classes still see it.
-
What does
readonlyon a field do?Answer
It allows one assignment, then rejects later ones
The field is set where it is declared or in the constructor, and a later assignment is a compile error.
-
What does this program print?
class User { public name: string = "Guest"; } const user = new User(); console.log(user.name);Answer
Guest
A public field is readable from outside, and it holds its initial value.
-
What does this program print?
class Vault { private code: string = "1234"; unlock(attempt: string): boolean { return attempt === this.code; } } const vault = new Vault(); console.log(vault.unlock("1234"));Answer
true
The method itself may read the private field, and the attempt matches it.
-
What does this program print?
class Animal { protected legs: number = 4; } class Dog extends Animal { describe(): string { return "Legs: " + this.legs; } } console.log(new Dog().describe());Answer
Legs: 4
The child class sees the protected field, so the method can build the text.
-
What does this program print?
class Config { readonly host: string = "localhost"; } const config = new Config(); console.log(config.host);Answer
localhost
A readonly field may be read freely; only a later assignment is rejected.
-
What does the compiler say about this program?
class Vault { private code: string = "1234"; } const vault = new Vault(); console.log(vault.code);Answer
error TS2341: Property 'code' is private and only accessible within class 'Vault'.
The private wall covers reads as well as writes, so the field is not reachable from outside.
-
What does the compiler say about this program?
class Config { readonly host: string = "localhost"; } const config = new Config(); config.host = "example.com"; console.log(config.host);Answer
error TS2540: Cannot assign to 'host' because it is a read-only property.
The field is readonly, so the assignment after creation stops the build.
Article author: Arthur Isaev