Reputation: 8412
I've google but I can't quite find out how to tell the Typescript compiler to stop failing compilation when I import a dependency but don't use it.
Commenting out code for comparison is something I frequently do and having to delete/comment out the corresponding imports has become a bit of a pain, especially when most times I'll add the code back a few seconds later.
Can anyone tell me how to do this?
I'm using create-react-app-typescript.
Upvotes: 1
Views: 2777
Reputation: 129
Typescript warnings that comes from strict-rules family won't cause your application to fail when building, Typescript errors are there to warn you about possible bugs in your script and make your code cleaner than plain Javascript.
Typescript warns you about unused imports in your file because in your tsconfig.json file option noUnusedLocals
is set to true
, this means a local variable* must be read|written|exported from the script that uses or initializes it.
@ts-expect-error
at one line above the error, this would do but in the future, if there is a possibility that said variable will be used in the same file, this line would throw yet another error@ts-ignore
at one line above the error, this will result in same behavior with @ts-expect-error
but even though variable used in the future, this would do nothingEditing JSON configuration will result in global update, that means unlike comment configurations
, this will affect every (not every, see include
option) typescript file in your project
Go to your projects tsconfig.json
file (probably at the root of your project), find noUnusedLocals
key and set its value to false
You do not need to set property noUnusedParameters
to false, it has nothing to do with this error.
PS: This answer doesn't provide any different solution from other answers, its purpose is to make the given solutions more readable/understandable by providing some information about the cause of the error.
Upvotes: 0
Reputation: 276239
I frequently do and having to delete/comment out the corresponding imports has become a bit of a pain, especially when most times I'll add the code back a few seconds later.
Its okay to have TypeScript errors as you still get output JavaScript (with default options). The errors exist just to make sure you don't commit and cause confusion for others.
Don't recommend this but here you go:
Use //@ts-ignore
to silence the error.
You can change the tsconfig
options i.e. "noUnusedLocals": false,
as well as "noUnusedLocals": false,
.
Upvotes: 0
Reputation: 21
Set compiler option in 'tsconfig.json'.
{
"compilerOptions": {
...
"noUnusedLocals": false,
"noUnusedParameters": false
}
}
Upvotes: 2
Reputation: 7575
There is now support for @ts-ignore. This appears to be merged into the master branch, but not sure which TS release its available in, so in theory, this should work:
// @ts-ignore: unused
/// <reference path="../scripts/typedefinitions/royalslider.d.ts" />
Upvotes: 0