Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript: @ts-ignore Not Working on Let Variable That's Not Reassigned?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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)

  1. 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?

  2. 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading