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
|
1 Answer
Reset to default 0You 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;
RecBase
with aRecOrder
because yourRecOrder
requires more specialized arguments – apokryfos Commented Mar 8 at 8:21