打字稿“新”表达式,其目标缺少构造签名,隐含具有“任何”类型

Posted

技术标签:

【中文标题】打字稿“新”表达式,其目标缺少构造签名,隐含具有“任何”类型【英文标题】: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 的语法错误 【参考方案1】:

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

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

【讨论】:

有了这个声明,我得到了错误:Property 'Go' does not exist on type 'typeof globalThis'.

以上是关于打字稿“新”表达式,其目标缺少构造签名,隐含具有“任何”类型的主要内容,如果未能解决你的问题,请参考以下文章

使用 node-fetch 时无法调用其类型缺少调用签名的表达式

反应/打字稿:参数“道具”隐含了“任何”类型错误

打字稿:未找到导出/JSX 元素类型没有任何构造或调用签名

编译打字稿时如何防止错误“对象类型的索引签名隐式具有'任何'类型”?

查找累积和反应打字稿

使用 require 时出现打字稿错误 - 此表达式不可调用。类型“typeof import(...)”没有调用签名.ts(2349)