在 Typescript 中使用断言并出现 'Assertions require every name in the call target to be declared with an explicit type annotation' 错误

Using assert in Typescript and getting 'Assertions require every name in the call target to be declared with an explicit type annotation' error

我是 Typescript 的新手,我找不到任何有用的方法来解决这个错误:

error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.

我使用的是一个足够简单的用例

import * as assert from 'assert'
import someFunction from './someFunction.js'

function test() {
  assert.strict.equal(someFunction(1, 1), 2)
}

test()

我已经尝试在文件顶部添加 /** @type {any} */,但我仍然看到此错误。我希望得到任何关于我可以做些什么的建议。

我认为不应通过 import * 导入此库。如果您只是:

,它将如您所愿地工作:
import assert from 'assert'

import * as Foo from 'foo' 会将所有命名的导出包装到一个对象中,其中 import Foo from 'foo' 将只采用默认导出。使用哪个取决于您导入的代码的结构。但是,一般来说,除非文档另有说明,否则最好从 import Foo from 'foo' 开始。

See working example on the Typescript playground

更多信息: