Typescript `super` 实现不提供值

Typescript `super` implementation not providing the value

我正在尝试从 extended class 打印 name。但是出现错误:Property 'name' does not exist on type 'Employee'.

class Person {
    #name:string;
    getName(){
        return this.#name;
    }
    constructor(name:string){
        this.#name = name;
    }
}

class Employee extends Person {

    #salary:number;
    constructor(name:string, salary:number = 0){
        super(name);
        this.#salary = salary;
    }
    async giveRaise(raise:number):Promise<void> {
        this.#salary += raise;
        await this.#storySalary;
    }

    pay():void {
        console.log(`¤ ${this.#salary} is paid to ${this.name}.`);
    }
    async #storySalary():Promise<void> {
        console.log(`Salary ¤ ${this.#salary} is stored for ${this.name}`);
    }
}

const NewEmployee = new Employee('Sha', 300);
console.log(NewEmployee.getName(), NewEmployee.pay())

Live Demo

#name:string;表示name是私有的属性,不能扩展私有变量。

您可能正在寻找的是 protected name: string

class Person {
    protected name:string;
    getName(){
        return this.name;
    }
    constructor(name:string){
        this.name = name;
    }
}