【问题标题】:Argument of type '{}' is not assignable to parameter of type 'Uint8Array'“{}”类型的参数不可分配给“Uint8Array”类型的参数
【发布时间】:2020-03-20 00:01:36
【问题描述】:

我正在尝试克隆和运行一个开源项目 repo,并且很难解决这个问题,npm start 失败并出现“编译失败错误”,并说明了以下原因。

“{}”类型的参数不能分配给“Uint8Array”类型的参数

    const [encChallenge] = await waitEvent(socket, 'data') 
    const challenge = decrypt(encChallenge) //This line causes the error

decrypt函数之后


  /** Decrypt data used shared key */
  function decrypt(data: Uint8Array) {
    if (!sharedKey) return null
    const nonce = data.slice(0, sodium.crypto_box_NONCEBYTES)
    const box = data.slice(sodium.crypto_box_NONCEBYTES, data.length)
    const msg = crypto.decrypt(box, nonce, sharedKey)
    return msg
  }

将参数更改为any 解决了它,但我不能这样做, 如何将我的参数转换为 Unit8Array?

【问题讨论】:

  • 答案很简单,encChallenge 不是来自Uint8Array 类型,但您的方法decrypt 需要一个。 waitEvent 自然返回什么类型?

标签: node.js typescript npm yarnpkg es6-modules


【解决方案1】:

只要encChallenge 是一个类型化数组,代表一个 8 位无符号整数数组,那么你应该能够做到:

const [encChallenge] = await waitEvent(socket, 'data') 
const challenge = decrypt(new Uint8Array(encChallenge);)

真的,我会让waitEvent encChallengewaitEvent 方法中成为强类型Uint8Array,然后它被抽象出来,如果你再次重用它,它总是那个类型。

【讨论】:

  • 感谢您的回答,由于某种原因,我应该可以做到。
猜你喜欢
  • 2019-06-24
  • 2021-06-14
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
  • 2019-10-17
相关资源
最近更新 更多