'search' was declared but never used


#1

Hello guys,

I’m building another search engine on Ionic.

I figured whenever i use this command I get an error message.

    this.searchControl.valueChanges.debounceTime(700).subscribe(search => {
 
      
      this.setFilteredItems();

  });

‘search’ was declared but never used.

There’s absolutely nothing wrong with this line of code. search is necessary and it should work when I search. The problem is because of this error message, the whole app doesn’t start on ionic lab.
Is there any way to bypass this error message? because this is not an error.

Thanks,


#2

No it isn’t. That’s exactly what the warning is saying. What do you want it to do? Because it isn’t doing it.


#3

hmm…
Without that line of code, it will not let me search.

  ionViewDidLoad() {
    this.setFilteredItems();
    this.searchControl.valueChanges.debounceTime(700).subscribe(search => {
 
      
      this.setFilteredItems();

  });
  }

and I was following this guideline here which tells me to include this line of command in ionViewDidload:

But Typescript sees that ‘search’ as an unnecessary command.
When I have two search engines built within a single app, it will not start ionic lab due to having two errors.
I have no idea what the problem is…or is there any other way to work around it?

Ionic lab works when I remove that command and restart and then add it again.

Thanks in advance.


#4
search 

Isn’t being accessed by anything. If you want to use the returned value of your subscription, your code would look like this

ionViewDidLoad() {
 this.searchControl.valueChanges.debounceTime(700).subscribe(search => {
 this.setFilteredItems(search: string)
 });
}
setFilteredItems(searchTerm: string) {
 this.filteredItems = this.unfilteredItems.filter((item: CustomType) => item._tag === searchTerm)
 /* or something like that */
 }

As of now you’re subscribing to an emitted value and completely disregarding it.

Edit: modified my answer a bit after reading the post you referenced


#5

@jamesharvey:

The standard syntax for “no matter what the value is, do this” looks like

_ => console.log('always do this')

or

() => console.log('always do this')

If you name the parameter in an arrow function, Typescript expects you to use it.

search => console.log('the value of search is', search);


#6

I will try that solution. Thanks,
@AaronSterling I’m kinda puzzled because I think it worked well before I upgrade Angular version a few times.


#7

Sorry, nothing worked.
It’s so strange because in previous version of angular, it worked well. I think that’s why the tutorial from 2017 doesn’t have any fix for this.

The app works when I remove the codes inside ionViewDidLoad and start then add the codes back again.
The app would be still running without saying ‘search was declared but never used’. This only happens when I start the app with the code.

search => {this.setFilteredItems()

should be used in ionViewDidLoad to make search bar work…


#8

Maybe changing the type from string to

string | null

Will work.

It’s an error I’ve not run into, but I’ve also never subscribed to a Form Control’s output.

Maybe try moving the subscription from did load to ionViewDidEnter?


#9

@joshmorony FYI:

I just clicked through to the tutlorial and the code looks broken. Perhaps a line went missing after an update. searchTerm is basically declared but never used. You need to set searchTerm to the value search, in order for everything to work.


How to stop 'was declared but never used' error detector
#10

I changed this configuration in tsconfig.json from true to false:

"noUnusedParameters": false,
and now typescript stopped throwing “was declared but never used” error.

In my current project, these unused parameters are necessary and even if they’re errors, they’re not supposed to stop the whole app from starting. I see this is a problem from typescript.

So Angular needs that ‘search =>’ to run search filter… but typescript detects it as an unused parameter. In case you guys see this error, you can turn off error detector in tsconfig.json file.


#11

This is not true. Your changing of tsconfig.json is encouraging you to continue in your error.


#12

As Aaron alluded to, all you needed to was:

this.searchControl.valueChanges.debounceTime(700).subscribe(_ => {
  this.setFilteredItems();
});

Since it is an unused parameter, and so the convention (recognized by the linter as well) is to use an underscore for those.


#13

Why is search not being used? Is another variable being used as the search term?


#14

Replacing search with an underscore works. I didn’t think about that usage…

Thanks,


#15

Does this work? I’m just curious.

function() {
this.setFilteredItems();
}