【问题标题】:TS2345: Argument of type X is not assignable to parameter of type YTS2345:X 类型的参数不可分配给 Y 类型的参数
【发布时间】:2018-02-09 20:57:48
【问题描述】:

TypeScript 出现奇怪错误:

如图所示,错误是:

TS2345:“ErrnoException”类型的参数不可分配给 '(err: ErrnoException) => void' 类型的参数。类型 'ErrnoException' 不提供与签名 '(err: ErrnoException): void'。

这是导致错误的代码:

export const bump = function(cb: ErrnoException){
  const {pkg, pkgPath} = syncSetup();
  fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), cb);
};

有人知道这里发生了什么吗?

【问题讨论】:

    标签: node.js typescript typescript2.3


    【解决方案1】:

    您正在发送一个 ErrnoException 类型的值,而您正在调用的函数需要一个接受 *ErrnoException** 类型参数并返回 void 的函数。

    你发送:

    let x = new ErrnoException;
    

    而你调用的函数期望

    let cb = function(e: ErrnoException) {};
    

    你可以改变你的函数来接收这样的正确参数。

    export const bump = function(cb: (err: ErrnoException) => void){
      const {pkg, pkgPath} = syncSetup();
      fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), cb);
    };
    

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 2021-03-02
      • 2022-11-21
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 2018-10-19
      • 1970-01-01
      相关资源
      最近更新 更多