最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Stuck on error TS2612 - How can I overwrite a non-abstract base property - Stack Overflow

programmeradmin4浏览0评论

There is a non-public library I haven't worked with in a while, and recently I tried using it in a TypeScript project that is targeting es2022. Several classes that used to transpile file are now throwing TypeScript error:

TS2612: Property goods will overwrite the base property in RecBase. If this is intentional, add an initializer. Otherwise, add a declare modifier or remove the redundant declaration.

I understand the suggested solutions, but neither are valid for my situation; I need to be able to instantiate the base class as well as the subclass which rules out abstract. From my limited TypeScript knowledge, declare will prevent the compiler from generating JS code for the subclass property.

I'm stuck. Any ideas on how I can solve this (other than targeting something before es2022)?

Here's a complete example that illustrates my predicament:

//  RecBase and ListBase cannot be abstract classes (i.e., I need to be able to create instances of them)
class RecBase {
  id: string
  goods: ListBase
}

class ListBase {
  lineNumber: number
}

//  This pair of subclasses extends the base classes with additional, domain specific properties
class RecOrder extends RecBase {
  orderID: string

  //  Note: I want to overwrite the base class goods, I don't want 'goods2' or 'goodsEx'
  goods: OrderList
}

class OrderList extends ListBase {
  price: number
}

There is a non-public library I haven't worked with in a while, and recently I tried using it in a TypeScript project that is targeting es2022. Several classes that used to transpile file are now throwing TypeScript error:

TS2612: Property goods will overwrite the base property in RecBase. If this is intentional, add an initializer. Otherwise, add a declare modifier or remove the redundant declaration.

I understand the suggested solutions, but neither are valid for my situation; I need to be able to instantiate the base class as well as the subclass which rules out abstract. From my limited TypeScript knowledge, declare will prevent the compiler from generating JS code for the subclass property.

I'm stuck. Any ideas on how I can solve this (other than targeting something before es2022)?

Here's a complete example that illustrates my predicament:

//  RecBase and ListBase cannot be abstract classes (i.e., I need to be able to create instances of them)
class RecBase {
  id: string
  goods: ListBase
}

class ListBase {
  lineNumber: number
}

//  This pair of subclasses extends the base classes with additional, domain specific properties
class RecOrder extends RecBase {
  orderID: string

  //  Note: I want to overwrite the base class goods, I don't want 'goods2' or 'goodsEx'
  goods: OrderList
}

class OrderList extends ListBase {
  price: number
}
Share Improve this question asked Mar 8 at 7:16 scubastevescubasteve 2,8684 gold badges41 silver badges51 bronze badges 1
  • I think that would violate the Liskov Substitution principle. You will not be able to substitute any RecBase with a RecOrder because your RecOrder requires more specialized arguments – apokryfos Commented Mar 8 at 8:21
Add a comment  | 

1 Answer 1

Reset to default 0

You can use declare to set more narrow type than in the base class, so your solution could look like:

Playground

//  RecBase and ListBase cannot be abstract classes (i.e., I need to be able to create instances of them)
class RecBase {
  declare id: string
  declare goods: ListBase;
  constructor(props: Partial<RecBase> = {}){
    const {id = '1', goods = new ListBase} = props;
    Object.assign(this, {id, goods});
  }
}

class ListBase {
  lineNumber: number = 0
}

//  This pair of subclasses extends the base classes with additional, domain specific properties
class RecOrder extends RecBase {
  declare orderID: string
  declare goods: OrderList;
  constructor(props: Partial<RecOrder> = {}){
    const {orderID = '1', goods = new OrderList} = props;
    super(props);
    Object.assign(this, {orderID, goods});
  }
}

class OrderList extends ListBase {
  price: number = 0;
}

const order = new RecOrder;
console.log(order.goods.price); // number;
发布评论

评论列表(0)

  1. 暂无评论