繁体   English   中英

如何为包含特定功能数组的JSON对象编写接口

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM