我想为JSON对象创建一个接口。它具有名称未知的 n 键,每个值都是具有特定签名的函数数组。
// CLASSES
class Server {
// Private variables
mapping : IMapping = {}
// Public functions
public use(endPoint = "", handler : IHandler) : void {
// Check if the end point exists in the mapping
if(this.mapping[endPoint] === undefined) {
this.mapping[endPoint] = {
[endPoint] : [handler]
};
} else {
}
}
}
// INTERFACES
interface IMapping
{
[key : string] : IHandler[];
}
interface IHandler {
(req : object, res : object, next : object) : void;
}
我的代码失败:this.mapping[endPoint]
Type '{ [x: string]: IHandler[]; }' is not assignable to type 'IHandler[]'.
Property 'length' is missing in type '{ [x: string]: IHandler[]; }'.
答案 0 :(得分:0)
应该是:
this.mapping[endPoint] = [handler];