-
Notifications
You must be signed in to change notification settings - Fork 12.8k
"Ignore this error message" code fix in JSX results in rendering // @ts-ignore
#27552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please note: Unless we add new suppression forms (ie, inline), the only fix for this is disabling the quickfix when a valid suppression position cannot be produced. |
(unless we really think something like {/*
// @ts-ignore */} is OK?) |
Would 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, const s = `
Hello ${doesnotexist}`; gets fix-ignored as const s = `
Hello ${
// @ts-ignore
doesnotexist}`; |
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 |
Another (odd looking) variation which works:
|
how smart you are!!! |
Works for |
Yeah all those linting rules will be so happy about that syntax Doing this now 😐
|
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: {/* lol https://github.com/Microsoft/TypeScript/issues/27552#issuecomment-495830020
// @ts-ignore */ /* prettier-ignore */}
<MyComponent foo={{
a: 'prop',
with: 'lots a',
big: 'object',
that: 'forces',
prettier: 'to wrap',
}}
/> previously: {/* lol https://github.com/Microsoft/TypeScript/issues/27552#issuecomment-495830020
// @ts-ignore */}
<MyComponent
foo={{
a: 'prop',
with: 'lots a',
big: 'object',
that: 'forces',
prettier: 'to wrap',
}}
/> |
No idea if prettier will also complain about excessive spreads, but <MyComponent
{...{}/* lol https://github.com/Microsoft/TypeScript/issues/27552#issuecomment-495830020
// @ts-ignore */}
foo={{
a: 'prop',
with: 'lots a',
big: 'object',
that: 'forces',
prettier: 'to wrap',
}}
/> 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. |
Why is this closed? Did we just commit to the ugly solution? |
reopen please... |
Yep. The quickfix now does the ugly thing. "prettiness" isn't a concern when it comes to suppressions which, by all rights, should be exceptional events. We're pretty locked by what jsx syntax allows, so it really is what it is. |
We definitely committed to the fugly solution...but maybe not. Can we vote/agree on keeping this open? I’d love to tackle this in some free time but don’t want to waste time if the current solution is the preferred option. |
Agreed. I'm currently using the fugly solution because a 3rd party library that I rely on has incorrect typings in their latest code. Fugly solution works for now, but would be good to have a one-liner if possible. |
Sadly, there is no other way to get a comment in jsx. It's gotta be within |
Is there a separate issue to track the possibility of this? {/* @ts-ignore */}
{whatever} |
I personally think {/* @ts-ignore */}
{whatever} is the best and the most universal solution for this. Auto formatting tools (prettier, etc.) may screw up below hacks. Note: {/*
// @ts-ignore */} while this <
// @ts-ignore
SomeComponent /> is auto-formatted and becomes invalid (at least on my prettier settings) |
Based on the success of #38228 I think this landed in 3.9 🎉 |
I guess this is more of a JSX issue, but check this out: Let's say I have this: import * as React from 'react';
declare var SomeComponentFromLibrary: React.FC<{
children?: React.ReactElement
}>;
declare var MyComponent: React.FC<{
foo: string,
}>;
export default () => (
<SomeComponentFromLibrary>
{/* @ts-expect-error */}
<MyComponent />
</SomeComponentFromLibrary>
)
However, adding another element to the children of the Is it possible to create comments in JSX that don't get transformed into code? |
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 "}":
|
You can use like this too:
Works for me! |
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
Running the
Ignore this error message
orAdd '@ts-ignore' to all error messages
code fix inserts a// @ts-ignore
which satisfies the compiler.But,
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
Actual behavior:
where
// @ts-ignore
mistakenly gets rendered.Related Issues:
#25240
The text was updated successfully, but these errors were encountered: