You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript可选链分支覆盖率疑问:总分支数为何是4而非2?

TypeScript可选链分支覆盖率疑问

我在理解TypeScript可选链的分支覆盖率时遇到了问题。

我的代码如下:

type testingType = {
   b?: { a?: number };
};
 
export function example(input: testingType) {
   return input.b?.a;
}

测试用例(仅为生成覆盖率报告强制通过):

test('test', () => {
   example({});
   expect(1).toBe(1);
});

覆盖率报告显示分支覆盖率为3/4,我无法理解总分支数为何是4。按我的理解应该只有2个分支:

  • b已定义
  • b未定义

内容的提问来源于stack exchange,提问作者tpszhao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 02:37:01