Issue
BACKSTORY (feel free to skip to the problem)
I'm trying to destructure a response from an Apollo useLazyQuery
, and then change one variable. Without Typescript, it would be two simple lines:
let [someFunction, { data, loading }] = useLazyQuery(QUERY);
data = 'whatever'
However, that offends Typescript, because I'm making all the variables let
even though most don't change. So, I changed the code to destrucure most of the response as constant, with a single exception:
const stuff = useLazyQuery(QUERY);
let [_, { data }] = stuff; // get one variable as a non-constant
const [someFunction, { loading }] = stuff; // get the rest as constants
The problem is, I'm still getting TypeScript errors/warnings:
77:8 warning '_' is assigned a value but never used no-unused-vars
77:8 error '_' is never reassigned. Use 'const' instead prefer-const
Of course, _
is just a placeholder to help me destructure data
, so it's not supposed to be re-assigned ... but it is needed on the let
line to make the destructuring work.
PROBLEM
I would have thought I could tell Typescript "don't worry about _
, just ignore it" by adding an @ts-ignore
comment:
const stuff = useLazyQuery(QUERY);
// @ts-ignore
let [_, { data }] = stuff; // get one variable as a non-constant
const [someFunction, { loading }] = stuff; // get the rest as constants
But all that does is add a new warning about how I should use @ts-expect-error
instead of @ts-ignore
... it doesn't get rid of the warning/error about my _
variable.
QUESTIONS (Two Related Ones)
I thought
@ts-ignore
ignored all warnings/errors on the next line; can anyone explain how Typescript decides which warnings/errors@ts-ignore
actually ignores, and which it doesn't?Can anyone explain how I can make Typescript stop complaining about
'_' is never reassigned. Use 'const' instead
error (using@ts-ignore
or anything else) ... without giving up on basic language features like destructuring?
Solution
For clarification : above errors are coming from es-lint and not TS itself. You will need to disable that.
You can use:
// eslint-disable-next-line
let [_, { data }] = stuff;
or
let [_, { data }] = stuff; // eslint-disable-line
Answered By - Tushar Shahi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.