TypeScript: "Ignore this error message" code fix in JSX results in rendering `// @ts-ignore`
TypeScript Version: 3.2.0-dev.20181004
Search Terms:
disableJsDiagnostics JSX Code fix Ignore this error message Add ‘@ts-ignore’ to all error messages
Code
// MyComponent.jsx
// @ts-check
import React from "react";
class MyComponent extends React.Component {
render() {
return (
<div>
// @ts-ignore
{doesNotExist}
</div>
);
}
}
export default MyComponent;
Running the Ignore this error message or Add '@ts-ignore' to all error messages code fix inserts a // @ts-ignore which satisfies the compiler.
But,
<div>
// @ts-ignore
{doesNotExist}
</div>
will actually render // @ts-ignore.
Expected behavior:
Looks like {/* @ts-ignore */} or {/* // @ts-ignore */} are not recognized as valid ignore comments.
So, the best I could come up with is
<div>
{/*
// @ts-ignore */}
{doesNotExist}
</div>
Actual behavior:
// MyComponent.jsx
// @ts-check
import React from 'react';
class MyComponent extends React.Component {
render() {
return (
<div>
// @ts-ignore
{doesNotExist}
</div>
);
}
}
export default MyComponent;
where // @ts-ignore mistakenly gets rendered.
Related Issues:
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 31
- Comments: 24 (5 by maintainers)
(unless we really think something like
is OK?)
awesome 🌹
Is there another pattern that others are using?
This is really really odd syntax
Edit the above doesn’t actually work.
How are people ignoring typescript errors inside of
TSXfiles today? I’ve done a ton of research and can’t find a single solution that works. Not being able to ignore some statement is a huge challenge.Another (odd looking) variation which works:
Why is this closed? Did we just commit to the ugly solution?
Yeah all those linting rules will be so happy about that syntax
Doing this now 😐
For anyone struggling with this issue but needing to do it inside of a JSX component, the format for comments is different when working inside of a tag. Specifically, drop the “{” and “}”:
Sounds like we’re open to it now: https://github.com/microsoft/TypeScript/issues/37738 PR here: https://github.com/microsoft/TypeScript/pull/38228 🎉
Is there a separate issue to track the possibility of this?
No idea if prettier will also complain about excessive spreads, but
should also work? At some point the prettier-ignore is just the better choice, though. There’s just not many options for comment locations inside jsx.
I ran into even more fun in typescript 3.7 in conjunction with prettier, because prettier keeps the attributes on a separate line, and now @ts-ignore must be positioned immediately before the property, not the start of the tag.
Here’s the workaround I have:
previously:
Based on the success of #38228 I think this landed in 3.9 🎉
Sadly, there is no other way to get a comment in jsx. It’s gotta be within
{}.reopen please…
Works for
.tsxwith Typescript 3.6.2Would be awesome to add new suppression forms, and even support for targeting specific errors. But, in the absence of that, we will be using that weirdo comment form since we need the ability to ignore errors in JSX constructs. It’s not pretty, but it’s the only thing that works. So, the fix could either (1) include it in this form or (2) not include it at all (so it doesn’t end up rendering). I like (1) even though its not pretty because it’s functionally correct - it would seem off if the rule could ignore everything except errors in the body of a JSX component. Moreover there is some precedent for odd-looking ignore comments in template strings. For example,
gets fix-ignored as