【问题标题】:Declare function as a parameter in Typings Declaration在 Typings Declaration 中将函数声明为参数
【发布时间】:2018-06-10 04:48:41
【问题描述】:

在我的globals.d.ts 中,我导出了多个函数,其参数也是函数。示例如下:

/**
 * Does something
 * @param {function(string): void} bar - a parameter
 * @returns {void}
 */
export function foo(bar: function(string): void): void;

ESLint 显示多个错误,指出:JSDoc types can only be used inside documentation comments.。我尝试通过从函数中删除类型并仅在 cmets 中声明它们来修复此错误,但随后参数的类型变为any (*)。我还尝试在.eslintrc.jsonfile 中添加"valid-jsdoc": "off" 规则,但没有成功。有没有办法在不丢失参数类型的情况下删除警告?

【问题讨论】:

  • 为什么不返回一个用字符串解析的承诺?
  • @Kulvar 这只是我的函数声明的一个简化示例,如果不是全部的话,大多数函数声明实际上都返回了其他东西,我不想返回一个包含两个承诺的对象和实际结果,因为它使事情变得复杂。此外,该解决方案不会解决有关 eslint 的问题。

标签: javascript visual-studio-code eslint typescript-typings


【解决方案1】:

您不能将参数类型指定为function(string): void。在 TypeScript 中定义函数类型只是语法错误。
而不是:
export function foo(bar: function(string): void): void;
做:
export function foo(bar: (arg: string) => void): void;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-23
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多