TypeScript JSDoc @type注解在链式变量声明中被忽略,

5ssjco0h  于 8个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(235)

我不确定这是个错误还是一个功能请求:

TypeScript 版本: 3.8.0-dev.20191206,在 3.7.2 中也得到了确认
搜索词: type ignored
代码

  1. var
  2. /** @type {string} */
  3. str1,
  4. /** @type {number} */
  5. num1;
  6. /** @type {string} */
  7. var str2;
  8. /** @type {number} */
  9. var num2;
  10. str1 = "Hello";
  11. num1 = "World"; // TSC does not complain => not OK
  12. str2 = "Hello";
  13. num2 = "World"; // TSC complains => OK

预期行为:

  1. test.js:15:1 - error TS2322: Type '"World"' is not assignable to type 'number'.
  2. 15 num1 = "World";
  3. test.js:18:1 - error TS2322: Type '"World"' is not assignable to type 'number'.
  4. 18 num2 = "World";

实际行为:

  1. test.js:18:1 - error TS2322: Type '"World"' is not assignable to type 'number'.
  2. 18 num2 = "World";

Playground 链接: 在 JS PlayGround 中无法复现任何与类型相关的警告,请在本地测试
相关问题:#22434 , #13371 (有些相关)

官方的 JSDoc 文档没有提到在一个 var 语句中混合类型的问题,但我认为如果 TS 符号能正常工作,那么 JS 的行为可能也是一样的。

  1. var str1: string,
  2. num1: number;
eh57zj3b

eh57zj3b1#

这不是我们寻找评论的地方,但我们可以添加它。老实说,如果每个变量上都有一个JS Doc类型的注解,我不明白为什么有人会这样写——你在$x^{var}$部分浪费了一个竖线,看起来更令人困惑🤷‍♂️

vc6uscn9

vc6uscn92#

@RyanCavanaugh 我必须承认这是一个自私的原因。我的客户的eslint设置需要将其链接到一个var语句中:-(

相关问题