Наследование объектов в Typescript - PullRequest
0 голосов
/ 28 сентября 2018

Я пытаюсь расширить класс, используя машинописный текст на угловом языке, и у меня появляется эта ошибка:

 export class Product {

  constructor(public id?: number, public name?: string,
     public brand?: string, public expiry?: Date) {

  }
}

export class StockItem extends Product {
  constructor(public id?: number, public name?: string,
     public brand?: string, public expiry?: Date, public category?: string,
     public price?: number, public quantity?: number,
     public sold?: number, public description?: string,
     public status?: boolean, public expired?: boolean,
     public addedOn?: Date)

      super(id, name, brand, expiry);
}

Строка ошибки:

src/app/models/data.model.ts(96,6): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(96,28): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(97,6): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(97,31): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(98,6): error TS2369: A parameter property is only allowed in a constructor implementation.
src/app/models/data.model.ts(99,6): error TS2391: Function implementation is missing or not immediately following the declaration.

1 Ответ

0 голосов
/ 28 сентября 2018

Вам не хватает {} для тела конструктора, в котором должен содержаться вызов super:

export class Product {

constructor(public id?: number, public name?: string,
    public brand?: string, public expiry?: Date) {

}
}
export class StockItem extends Product {
    constructor(id?: number, name?: string,
        brand?: string, expiry?: Date, public category?: string,
        public price?: number, public quantity?: number,
        public sold?: number, public description?: string,
        public status?: boolean, public expired?: boolean,
        public addedOn?: Date) {

        super(id, name, brand, expiry);
    }
}

Также нет необходимости повторно объявлять поля, объявленные базовым классом (т. Е. Я удалил public модификатор id, name, brand и expiry, поскольку они уже объявлены в базовом типе).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...