Classes are declared using the class
keyword
class Car {
price: number = 0; // field with initializer
color: string; // field without initializer, must be initialized in constructor
private owner: string; // field with visibility modifier
readonly producer: string; // field with readonly modifier
static isCar: boolean = true; // static field
isNew? : boolean; // optional field
method: (x: number) => number; // field with function type
constructor(color: string) { // constructor
this.color = color;
this.owner = "B";
this.producer = "C";
this.isNew = true;
this.method = (x: number) => x + 1;
}
}
class Car {
private speed_: number = 0;
mileage = 0;
// method with visibility modifier
public drive(second: number) {
this.mileage += this.speed_ * second;
}
// static method
static isCar() {
return true;
}
// getter
get speed() {
return this.speed_;
}
// setter
set speed(sp: number){
this.speed_ = sp;
}
}
class Car {
price: number = 0;
color: string;
constructor(color: string) {
this.color = color;
}
drive() {
console.log("drive");
}
}
class Bus extends Car {
height: number;
constructor(color: string, height: number) {
super(color);
this.height = height;
}
drive() {
console.log("bus drive");
super.drive();
}
}
let car = new Car("red");
derived class can be assigned to base class
let bus: Car = new Bus("blue", 10);
Functions defined inside non-static method can capture this
class A {
n: number = 10;
say(){
let anonymousFunc = () => console.log(this.n);
return anonymousFunc;
}
}
let a = new A();
a.say()(); // 10
-
class Car { // Not Supported constructor(private owner: string) { // field in constructor parameter list } }
-
let car = new Car(); // Not Supported car.drive = (second: number) => { }
-
class Car { // Not Supported drive(this: Car, second: number) { } }
-
// Not Supported let car = Car;
-
class MyClass { // Not Supported [s: string]: boolean | ((s: string) => boolean); check(s: string) { return this[s] as boolean; } }
-
In ts2wasm-compiler, classes are treated as nominal typing. This is because class names represent abstractions of real-world entities, and defining class relationships based on structure rather than names can introduce error-prone code.
class Person { name: string = ''; age: number = 0; } class Employee { name: string = ''; age: number = 0; salary: number = 0; } class Employee1 extends Person { salary: number = 0; } // Not Supported const p: Person = new Employee(); // OK const p1: Person = new Employee1();