'X' is declared but its value is never read in TypeScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

'X' is declared but its value is never read in TypeScript #

To solve 'is declared but its value is never read' error in TypeScript, prefix any unused parameter name with an underscore, or disable the noUnusedLocals and noUnusedParameters options in your tsconfig.json file to silence the error in your entire project.

declared but its value is never read

Here is an example of how the error occurs.

index.ts
// ⛔️ Error: 'message' is declared but its value is never read.ts(6133) function logMessage(message: string) { console.log('hello world'); } // 'myVariable' is assigned a value but never used. const myVariable = 'hello';

When dealing with function parameters, you can fix the error by prefixing the unused parameter with an underscore.

index.ts
function logMessage(_message: string) { console.log('hello world'); }
When using _myParam instead of myParam, you indicate to TypeScript that you don't intend to use the parameter.

You can also get around the error by using the // @ts-ignore comment to disable type checking for a single line.

index.ts
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore function logMessage(message: string) { console.log('hello world'); }

The // @ts-ignore comment disables all type checking errors on the next line.

If you use a linter, chances are you have to disable it for the line on which you use @ts-ignore as most linters have rules against using ts comments.

You can solve the "'X' is declared but its value is never read'" error by updating the configuration in your tsconfig.json file and disabling error reporting for unused local variables and unused parameters.

Open your tsconfig.json file and disable the following 2 options:

tsconfig.json
{ "compilerOptions": { "noUnusedLocals": false, "noUnusedParameters": false, // ... rest } }

The noUnusedLocals option disables error reporting for unused local variables.

The noUnusedParameters option disables error reporting for unused parameters in functions.

After updating your tsconfig.json file, make sure to restart your IDE and development server.

With the two options disabled, the following code doesn't cause an error:

index.ts
function logMessage(message: string) { console.log('hello world'); }

If you are getting the "'X' is assigned a value but never used" eslint warning, you can disable the warning for a single line with a comment:

index.ts
// eslint-disable-next-line @typescript-eslint/no-unused-vars const myVariable = 'hello';

You can also disable the warning for the entire file using a comment:

index.ts
/* eslint-disable @typescript-eslint/no-unused-vars */ const myVariable = 'hello';

Or you could disable the rule for your entire project by editing the rules object in your .eslintrc.js file.

.eslintrc.js
module.exports = { // ... rest rules: { 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'off', // ... rest }, };

The two lines above disable the no-unused-vars rule globally.

Conclusion #

To solve the 'is declared but its value is never read' error in TypeScript, prefix any unused parameter name with an underscore, or disable the noUnusedLocals and noUnusedParameters options in your tsconfig.json file to silence the error in your entire project.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.