Classes, implements and access modifiers
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Type class fields, constructors and methods, including parameter properties.
- Use implements to check a class against an interface.
- Apply public, protected, private and readonly correctly.
Overview
#Class members
class Point {
readonly x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
distance(): number {
return Math.hypot(this.x, this.y);
}
}
Parameter properties
A modifier on a constructor parameter declares and assigns the field in one step.
class User {
constructor(public name: string, private age: number) {}
}
implements
implements checks that the class has the interface shape. It does not add the members and does not change the runtime.
interface Named { name: string }
class Person implements Named { name = "Ada"; }
Access modifiers
public is the default. private is compile-time only and is not a runtime guarantee. protected allows subclasses. readonly blocks reassignment after construction. The ECMAScript #field syntax is the real runtime-private option.
abstract
An abstract class cannot be instantiated and may declare abstract members that subclasses must implement.
Runtime note
Modifiers and interfaces are erased. A private field is still a normal property in the emitted JavaScript unless you use #.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Does this compile? ```ts class Counter { private count = 0; } const c = new Counter(); console.log(c.count); ```
Exercise 2Implement
Implement an abstract class `Shape` with an abstract `area(): number` and a concrete `describe(): string` returning `area=` plus the area, then a `Square` subclass.
starter.typescriptexport abstract class Shape { abstract area(): number; describe(): string { // your code } } export class Square extends Shape { constructor(private size: number) { super(); } area(): number { // your code } }Reveal the reference answer
reference.typescriptexport abstract class Shape { abstract area(): number; describe(): string { return "area=" + this.area(); } } export class Square extends Shape { constructor(private size: number) { super(); } area(): number { return this.size * this.size; } }Authored expected output: Compiles with no errors. Runtime: new Square(2).describe() is "area=4".
The abstract method is implemented by the subclass, and the concrete method calls it polymorphically.
Exercise 3Debug
This fails to compile because the class does not satisfy the interface. Fix it. ```ts interface Serializable { toJSON(): string } class Item implements Serializable { constructor(public id: string) {} } ```
starter.typescriptinterface Serializable { toJSON(): string } class Item implements Serializable { constructor(public id: string) {} }Reveal the reference answer
reference.typescriptinterface Serializable { toJSON(): string } class Item implements Serializable { constructor(public id: string) {} toJSON(): string { return JSON.stringify({ id: this.id }); } }Authored expected output: Compile error resolved: Class 'Item' incorrectly implements interface 'Serializable'. Adding toJSON fixes it.
implements requires the class to provide every member of the interface.
Common misconceptions
#- private prevents access at runtime.
- implements copies the interface members into the class.
- Parameter properties create runtime-only fields.
Follow-up practice
#- typescript-declaration-files
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.