【问题标题】:Typescript 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type打字稿“新”表达式,其目标缺少构造签名,隐含具有“任何”类型
【发布时间】:2022-01-19 19:01:51
【问题描述】:

在我的 nextjs 项目中,我有以下代码:

const go = new global.Go()

global.Go 是通过wasm 定义的。

当我运行tsc 命令时,我收到以下错误:

error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

所以我创建了文件types/global.d.ts 并添加了这些行:

export declare global {
  function Go(): unknown
}

但现在我收到此错误:

error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.

我该如何解决这个问题?

【问题讨论】:

  • Class 是什么?
  • mmm,是any 的别名。我编辑问题。
  • 您只是想定义构造签名吗?您可以使用以下语法来做到这一点:export declare global { Go: { new (): unknown } }.
  • @CRice 我收到 Go: { new (): unknown } 的语法错误

标签: javascript typescript next.js type-declaration


【解决方案1】:

就像错误消息所说的那样;给它一个构造签名

export declare global {
  const Go: new () => Go
}

【讨论】:

  • 有了这个声明,我得到了错误:Property 'Go' does not exist on type 'typeof globalThis'.
猜你喜欢
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 2021-03-29
  • 2018-07-19
相关资源
最近更新 更多