【发布时间】:2020-05-01 17:47:59
【问题描述】:
所以我声明了一个同名的类和接口。
declare module "mongoose"
{
class Schema<T = any>
{
constructor(definition?: SchemaDefinition);
}
interface Schema<T = any>
{
new(definition?: TypedSchemaDefinition<T>): Schema<T>;
}
}
假设TypedSchemaDefinition 只是将类型参数道具转换为它们的运行时对应物。为了简单起见,我不会包含所有实现此功能的代码,除非需要。
示例道具:编译类型 => 运行时类型 & 字符串 => 字符串 & 数字 => 数字等...
这不应引发错误。
interface ShippingCompileType {
days: number,
price: number,
}
const ShippingRuntimeType: TypedSchemaDefinition<ShippingCompileType> = {
days: Number,
price: Number,
}
const ShippingSchema = new Schema(ShippingRuntimeType);
Error: TypedSchemaDefinition<ShippingCompileType> is not assignable to SchemaDefinition
我不知道这是错误还是预期的功能,因为 mixins 和声明合并应该合并两个构造函数类型并允许 ShippingRuntimeType 作为函数的有效参数。如果这是一个错误,那么有解决方法吗??
【问题讨论】:
标签: typescript mongoose declaration mongoose-schema mixins