[英]How to write an interface for a JSON object that contains an array of specific functions
我想為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]
with
Type '{ [x: string]: IHandler[]; }' is not assignable to type 'IHandler[]'.
Property 'length' is missing in type '{ [x: string]: IHandler[]; }'.
應該只是:
this.mapping[endPoint] = [handler];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.