告诉VSCode / TypeScript类型推断将Mocha`beforeEach`视为初始化?

问题描述

我有这样写的Mocha测试:

import sinon from 'sinon';
const sandbox = sinon.sandbox();

describe('some test',() => {
  let stub1;
  beforeEach(() => {
    stub1 = sandbox.stub(APICall,'method1');
  });

  it('...',() => {
    // `stub1` inferred to `any`
  });
});

如果我想让VS Code(我认为在后台使用TypeScript)推断stub1sinon.SinonStub,则必须使用JSDoc(或TypeScript)手动声明该事实:>

describe('some test',() => {
  /** @type {sinon.SinonStub} */
  let stub1;
  beforeEach(() => {
    stub1 = sandbox.stub(APICall,() => {
    // now `stub1` gets inferred to `sinon.SinonStub`
  });
});

是否有更优雅的方法来获取VSCode / TypeScript进行推断?声明每个存根的类型都是很多样板。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)