microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.01k stars 12.48k forks source link

Support `@ts-ignore` for suppressing errors in template literals #51600

Open rishipal opened 1 year ago

rishipal commented 1 year ago

At Google, we use @ts-ignore to suppress build errors when rolling out new TypeScript releases. However, we don't have a mechanism that we can apply in an automated way to suppress errors inside tagged template literals. This comes from the limitation that @ts-ignore only suppresses errors on the immediate next line, and for errors within a multi-line template the // @ts-ignore comments get consumed by the literal string itself.

Illustrative example with multi-line tagged template literals:

function bar(x: string) { console.log(x);}
function foo(x: string) {  console.log(x); }

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

This does not work either (attempt2):

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

Playground link with relevant code

Actual behavior

The error is not suppressed, and the // @ts-ignore comment is consumed in the template literal in attempt2.

Expected behavior

Ideally, the error gets suppressed in attempt1.

Search Terms

ts-ignore, tagged-template-literals

MartinJohns commented 1 year ago

As a workaround you can place the ts-ignore within the placeholder, but before your expression. That works.

Your suggestion would likely mean that ALL errors in the string are suppressed, something probably unwanted as well.

rishipal commented 1 year ago

That's an acceptable workaround.

bar`error in the call below get suppressed  
${// @ts-ignore
foo(true)} 
`
gunan commented 1 year ago

Would it be possible to introduce a new type of annotation, called ts-ignore-block (not too attached to the name). It could work as the initial suggeston in @rishipal 's suggestion:

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

What do you think @MartinJohns @RyanCavanaugh