[英]Typescript generics for array
I need to extend a generic list of Array which has been extended from my class.我需要扩展一个从我的 class 扩展而来的通用数组列表。 How to do it properly?
如何正确地做到这一点?
export interface DeliveryMethod {
readonly id: string;
readonly company: string;
readonly cost: number;
readonly threshold: number;
readonly intervals: Array<Interval>;
readonly paymentMethods: Array<PaymentMethod>;
}
export interface Delivery {
selected: SelectedDelivery;
available: { [key : string] : Array<T extends DeliveryMethod>};
}
Cannot find name 'T'.ts(2304)找不到名称“T”.ts(2304)
available: { [key : string] : Array<T extends DeliveryMethod>};
For example i need something like that:例如我需要这样的东西:
const Delivery = {
selected :{/*data inside*/},
available:{
pickup: [<Pickup>{},<Pickup>{}],
courier: [<Courier>{},<Courier>{}]
}
}
The @aopanasenko answer is fine. @aopanasenko 的答案很好。 I want to complete it adding a way to solve the problem of multiple specifications.
我想完成它添加一种解决多规格问题的方法。
If the properties of available
aren't much, and they're fixed and stable, then you could think about listing them all in the Delivery
interface:如果
available
的属性不多,而且它们是固定且稳定的,那么你可以考虑将它们全部列在Delivery
界面中:
interface Delivery<T extends DeliveryMethod, U extends DeliveryMethod> {
available: { [key : string] : Array<T | U>};
};
Then you can define the delivery
object like this:然后您可以像这样定义
delivery
object:
const delivery: Delivery<Pickup, Courier>
If you don't know the properties a priori, then you need a way to link a property name to a TypeScript type, for example I added an available
object just for the mapping:如果您不知道先验属性,那么您需要一种方法将属性名称链接到 TypeScript 类型,例如,我添加了一个
available
的 object 仅用于映射:
interface Delivery {
available: { [key: string]: Array<DeliveryMethod> | null };
};
const delivery: Delivery = {
available: {}
}
const available: {
pickup: Pickup[] | null,
courier: Courier[] | null
} = {
pickup: null,
courier: null
};
delivery.available = { ...delivery.available, ...available };
In this way, it's correctly type-checked.通过这种方式,它得到了正确的类型检查。 I also added
| null
我还添加了
| null
| null
in order to provide an example, you can remove it. | null
为了提供一个例子,可以去掉。
Update更新
As mentioned by @AndreaSimoneCosta in comment the following code should work:正如@AndreaSimoneCosta 在评论中提到的,以下代码应该可以工作:
export interface DeliveryMethod {
readonly id: string;
readonly company: string;
readonly cost: number;
readonly threshold: number;
readonly intervals: Array<Interval>;
readonly paymentMethods: Array<PaymentMethod>;
};
export interface Delivery<T extends DeliveryMethod = DeliveryMethod> {
selected: SelectedDelivery;
available: {
[key : string] : Array<T>
};
};
You should define generic type parameter list in angle brackets following the name of the Delivery
interface:您应该在
Delivery
接口名称之后的尖括号中定义泛型类型参数列表:
export interface DeliveryMethod {
readonly id: string;
readonly company: string;
readonly cost: number;
readonly threshold: number;
readonly intervals: Array<Interval>;
readonly paymentMethods: Array<PaymentMethod>;
};
export interface Delivery<T extends DeliveryMethod> {
selected: SelectedDelivery;
available: {
[key : string] : Array<T>
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.