typescript - 如何在 Typescript 接口(interface)中为函数定义返回类型 void?

标签 typescript

我有这个功能:

   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),但它对我不起作用:

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

我收到一条消息说“unexpected token”无效

最佳答案

函数类型接口(interface)成员的语法有两个选项,它们在这里是等价的:

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

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

关于typescript - 如何在 Typescript 接口(interface)中为函数定义返回类型 void?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988956/

相关文章:

typescript - vue-class-component 中的类型不存在属性

javascript - 在 cypress 中加载时继续向下滚动

angular - 如何在 Angular Universal 中导入 Material Module 或 NgxBootstrap?

Javascript - 对象构造和利用 - 深入理解

typescript - 在子元素中使用 ngFor 的局部变量

php - 通过使用 PHP 搜索特定记录的另一个列值来获取 MySql DB 表列值

javascript - Angular2 - 从属性方法访问组件属性

javascript - 如何在 MacOS 上的 Firefox 中不触发 Ctrl+LeftMouseClick 鼠标右键单击?

javascript - Vue 3 - 如何重命名通过 toRefs() 公开的保留关键字?

typescript - 如何将对象键的类型限制为命名空间常量?