Вы не инициализировали значения свойств, поэтому ожидается, что они будут нулевыми.
Это будет работать, как и ожидалось:
class Test {
public property1: string = "test";
public property2: number = 123;
test() {
console.log(this);
console.log(Object.getOwnPropertyNames(this));
}
}
const a = new Test();
a.test();
Лучший способ go об этом создать конструктор для класса Test и при создании нового Test () передать нужные значения в виде параметров
Пример:
class Test {
private property1: String;
private property2: number;
constructor(property1: String, property2: number) {
this.property1 = property1;
this.property2 = property2;
}
test() {
console.log(this);
console.log(Object.getOwnPropertyNames(this));
}
}
const a = new Test("test", 123);
a.test();