JOURNEY TO THE PROBLEM
My current task involves destructuring a response obtained from an Apollo useLazyQuery
, with the intention to modify one variable. In a non-Typescript environment, achieving this would be straightforward with just two lines of code:
let [someFunction, { data, loading }] = useLazyQuery(QUERY);
data = 'whatever'
However, this approach raises issues with Typescript as all variables are declared using let
even though most remain constant. To address this, I modified the code to destructure the majority of the response as constants, except for one variable:
const stuff = useLazyQuery(QUERY);
let [_, { data }] = stuff; // setting one variable as a non-constant
const [someFunction, { loading }] = stuff; // retrieving the rest as constants
The challenge lies in the fact that I am still encountering warnings/errors from TypeScript:
77:8 warning '_' is assigned a value but never used no-unused-vars
77:8 error '_' is never reassigned. Use 'const' instead prefer-const
While _
serves as a placeholder during the destructuring process and should not be re-assigned, it is necessary on the let
line to facilitate the operation.
ENCOUNTERED ISSUE
I assumed that by adding an @ts-ignore
comment, I could inform TypeScript to disregard the concerns related to _
:
const stuff = useLazyQuery(QUERY);
// @ts-ignore
let [_, { data }] = stuff; // setting one variable as a non-constant
const [someFunction, { loading }] = stuff; // retrieving the rest as constants
Instead, this action led to a fresh warning advising the usage of
@ts-expect-error</code rather than <code>@ts-ignore
, without resolving the issue regarding the _
variable.
QUERIES (Linked Questions)
I was under the impression that
@ts-ignore
dismisses all warnings/errors on the subsequent line. Can someone clarify how TypeScript determines which warnings/errors to overlook when@ts-ignore
is used?Could someone provide insight on how to eliminate the complaint about the
error in TypeScript (whether through'_' is never reassigned. Use 'const' instead
@ts-ignore
or another method) while still benefiting from features like destructuring?