TypeScript Support @ts-ignore for suppressing errors in template literals

dsf9zpds  于 10个月前  发布在  TypeScript
关注(0)|答案(3)|浏览(86)

在谷歌,我们使用 @ts-ignore 来抑制在推出新的 TypeScript 版本时构建错误。然而,我们没有一种机制可以自动应用以抑制标记模板字面量内的错误。这源于 @ts-ignore 只抑制紧接在下一行的错误,而对于多行模板中的错误,// @ts-ignore 注解会被字面字符串本身消耗掉。
示例:

  1. function bar(x: string) { console.log(x);}
  2. function foo(x: string) { console.log(x); }
  3. // @ts-ignore
  4. bar`error in the line below
  5. ${foo(true)}
  6. is not suppressed`

这也不起作用(尝试2):

  1. bar`error in the line below
  2. // @ts-ignore
  3. ${foo(true)}
  4. is not suppressed`

带有相关代码的 Playground 链接

实际行为

错误没有被抑制,并且在尝试2中,// @ts-ignore 注解被模板字符串消耗掉了。

预期行为

理想情况下,尝试1中的错误应该被抑制。

搜索关键词

ts-ignore,标记模板字面量

w41d8nur

w41d8nur1#

作为解决方法,你可以在占位符内放置ts-ignore,但在表达式之前。这样可以工作。
你的建议可能意味着字符串中的所有错误都被抑制了,这可能是不想要的。

xt0899hw

xt0899hw2#

这是一个可接受的解决方法。

kcugc4gi

kcugc4gi3#

是否可以引入一种新的注解类型,称为ts-ignore-block(不太依赖于名称)。它可以作为@rishipal建议中的初始建议:

  1. // @ts-ignore-block
  2. now` all errors in these lines including
  3. ${foo(true)}
  4. are all suppressed`

你怎么看@MartinJohns@RyanCavanaugh

相关问题