繁体   English   中英

如何在Typescript接口中为函数定义void的返回类型?

[英]How can I define a return type of void for a function in a Typescript interface?

我有这个功能:

   network = (action): void =>{
        if (action) {
            this.action = action;
            this.net = true;
            this.netd = true;
        } else {
            this.action = null;
            this.net = false;
            this.netd = false;
        }
    }

我试图定义一个界面,但它不适合我:

interface IStateService {
    network: (action: string): void;
}

我在消息上收到一条消息“意外令牌”

对于函数类型的接口成员,您有两种语法选项,它们在此处等效:

interface IStateService {
    network: (action: string) => void;
}

要么

interface IStateService {
    network(action: string): void;
}

这接近于“type literal”语法,但是需要大括号:

interface IStateService {
    network: { (action: string): void; }
}

这是完整的语法,并允许定义重载,如下所示:

interface IStateService {
    network: {
        (): string;
        (action: string): void;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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