typescript - 在扩展 medusa js 中的客户服务列表方法时遇到返回类型问题
我想在客户列表响应中添加另一个字段,用于显示客户的状态,即客户是活跃、已停用还是已删除。我的代码在响应中与其他客户字段一起发送了一个 customerStatus 字段,但在我的 updatedResult 上显示插入前错误,在最后一行 return [updatedResult, count]。
这是我的一段代码
async listAndCount(
selector: Selector<Customer> & { q?: string; groups?: string[] },
config: FindConfig<Customer> = {
relations: [],
skip: 0,
take: 50,
order: { created_at: "DESC" },
}
): Promise<[Customer[], number]> {
const [result, count]: [Customer[], number] = await super.listAndCount(selector, config);
let customerStatus = '';
const updatedResult = result.map(data => {
if(data.isActive === true && data.isDeleted === false)
customerStatus = 'Active';
else if (data.isActive === false && data.isDeleted === false)
customerStatus = 'Deactivated';
else if (data.isActive === false && data.isDeleted === true)
customerStatus = 'Deleted';
return { ...data, customerStatus: customerStatus };
});
return [updatedResult, count] ;
}
}
它显示的错误如下所示:
Type '{ customerStatus: string; isActive: boolean; isDeleted: boolean; email: string; first_name: string; last_name: string; billing_address_id: string; billing_address: Address; shipping_addresses: Address[]; ... 9 more ...; updated_at: Date; }[]' is not assignable to type 'Customer[]'.
Property 'beforeInsert' is missing in type '{ customerStatus: string; isActive: boolean; isDeleted: boolean; email: string; first_name: string; last_name: string; billing_address_id: string; billing_address: Address; shipping_addresses: Address[]; ... 9 more ...; updated_at: Date; }' but required in type 'Customer'.ts(2322)
customer.d.ts(18, 13):'beforeInsert' 在这里声明。
此外,这个 beforeInsert 方法在 customer 类中是私有的,它不允许我更改 listAndCount 方法的返回类型。
我尝试更改返回类型并查看插入前的方法,但它是私有的。返回语句中的 updatedResult 显示错误,但它并没有阻止我运行代码。