如何解决“断言要求呼叫目标中的每个名称都使用显式类型批注.ts2775声明”? 如何解决此错误?

问题描述

我下面有JavaScript代码,我正在使用TypeScript编译器(TSC)按照Typescript Docs JSDoc Reference进行类型检查。

const assert = require('assert');
const mocha = require('mocha');

mocha.describe('Array',() => {
    mocha.describe('#indexOf()',() => {
        mocha.it('should return -1 when the value is not present',/** */
        () => {
            assert.strictEqual([1,2,3].indexOf(4),-1);
        });
    });
});

我看到此错误

Assertions require every name in the call target to be declared with an explicit type annotation.ts(2775)
SomeFile.test.js(2,7): 'assert' needs an explicit type annotation.

如何解决错误

解决方法

您需要为assert变量添加JSDoc类型的注释,例如下面的示例。如果愿意,您可以添加比{any}更具体的类型。

/** @type {any} */
const assert = require('assert');

有关更多信息,请参见this Github issue