Typescript 在其他接口中扩展数组的自定义接口
我在编译过程中不断遇到错误: 错误TS2339类型“Action[]”上不存在属性“customMethod” 以下是我的界面:Typescript 在其他接口中扩展数组的自定义接口,typescript,interface,Typescript,Interface,我在编译过程中不断遇到错误: 错误TS2339类型“Action[]”上不存在属性“customMethod” 以下是我的界面: export interface Action { Name: string; someFunc(): void; } export interface ActionCollection { Actions: Action[]; } 然后,在我的代码中,我尝试使用一个尚未在接口中描述但在运行时可用的方法。此方法可以从ActionsColle
export interface Action {
Name: string;
someFunc(): void;
}
export interface ActionCollection {
Actions: Action[];
}
然后,在我的代码中,我尝试使用一个尚未在接口中描述但在运行时可用的方法。此方法可以从ActionsCollection中的Actions数组中获得,就像本机.length属性一样
let myAC: ActionCollection = new ActionCollection( stuff );
myAC.Actions.customMethod(); // Note that it is attached to Actions
我的问题是如何在接口中定义它
我尝试过类似的方法,但我遇到了错误:
export interface Action<> {
customMethod(): any;
}
导出接口操作{
customMethod():任意;
}
如果希望操作数组具有此方法,则需要将其添加到数组
界面:
interface Array<T> {
customMethod(): void;
}
但这可能不是您想要的,而是定义您自己的数组:
export interface Action {
Name: string;
someFunc(): void;
}
export interface ActionsArray extends Array<Action> {
customMethod(): void;
}
export interface ActionCollection {
Actions: ActionsArray;
}
导出接口操作{
名称:字符串;
someFunc():void;
}
导出接口ActionsArray扩展阵列{
customMethod():void;
}
导出接口ActionCollection{
行动:行动Sarray;
}
您说您已经实现了实际的函数,所以这应该足够了。如果您希望actions数组具有此方法,则需要将其添加到
数组
界面:
interface Array<T> {
customMethod(): void;
}
但这可能不是您想要的,而是定义您自己的数组:
export interface Action {
Name: string;
someFunc(): void;
}
export interface ActionsArray extends Array<Action> {
customMethod(): void;
}
export interface ActionCollection {
Actions: ActionsArray;
}
导出接口操作{
名称:字符串;
someFunc():void;
}
导出接口ActionsArray扩展阵列{
customMethod():void;
}
导出接口ActionCollection{
行动:行动Sarray;
}
你说你已经实现了实际的功能,所以这就足够了。你不应该做
newactioncollection
因为ActionCollection
是一个接口,你不会在那里出错吗?你不应该做newactioncollection
因为ActionCollection
是一个接口,你没有发现错误吗?