Grokking TypeScript
0% completed
Readonly Properties
The compiler accepts the first assignment below and rejects the second, although both change a property of the same object:
class BankAccount { readonly accountNumber: string; owner: string; constructor(accountNumber: string, owner: string) { this.accountNumber = accountNumber; this.owner = owner; } } const account = new BankAccount("AC-1001", "Alice"); account.owner = "Alice Khan"; // Allowed account.accountNumber = "AC-2002"; // Error
error TS2540: Cannot assign to 'accountNumber' because it is a read-only property.
The `readonly
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%