Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 允许对象上的未知方法_Typescript_.d.ts - Fatal编程技术网

Typescript 允许对象上的未知方法

Typescript 允许对象上的未知方法,typescript,.d.ts,Typescript,.d.ts,我有一个对象,它有多个在编译时未知的方法名。但是,方法的签名总是相同的。我尝试使用如下索引类型: interface List<T> extends Array<T> { [key: string]: (...arg0: any[]) => List<T> [key: string]: () => List<any> each(fn: Function): void each(...args: any[]

我有一个对象,它有多个在编译时未知的方法名。但是,方法的签名总是相同的。我尝试使用如下索引类型:

interface List<T> extends Array<T> {
    [key: string]: (...arg0: any[]) => List<T>
    [key: string]: () => List<any>
    each(fn: Function): void
    each(...args: any[]): void
    toArray(): T[]
}
接口列表扩展了数组{
[键:字符串]:(…arg0:any[])=>列表
[键:字符串]:()=>列表
每个(fn:功能):无效
每个(…args:any[]):无效
toArray():T[]
}
但是,typescript抱怨说它无法将其他方法签名放入其中:

类型为“{(fn:Function):void;(…args:any[]):void;}”的属性“each”不能分配给字符串索引类型“(…arg0:any[])=>List”

我知道这是一件不寻常的事情,但如何才能在typescript中实现我所需的功能?

界面列表{
interface List<T>{
[key: string] : ((...arg) => any) | (() => List<any>);
each: ((fn: Function) => void) | ((...args: any[]) => void);
toArray : () => T[];
[键:字符串]:((…arg)=>any)|(()=>List); 每个:((fn:Function)=>void)|((…args:any[])=>void); toArray:()=>T[];
}


这就是您想要的吗?

合并所有这些声明有什么原因吗?是的,您在同一个接口中定义了两次相同的属性,因此在typescript中,合并它时可以提到具有重载的多个相同属性类型是很常见的。合并声明只会使它更难阅读