Skip to content
Advertisement

How to solve Argument of type ‘boolean’ is not assignable to parameter of type ‘string’ error in setAttribute() function

I have a function which dynamically updates a HTML aria-expanded attribute to true or false. However when I type element as HTMLElement, I receive a Argument of type 'boolean' is not assignable to parameter of type 'string'

  expandEotyDocumentsPanel(element: HTMLElement) {
    this.eotyExpanded = !this.eotyExpanded;
    element.setAttribute('aria-expanded', this.eotyExpanded);
  }

As you might have already noticed, this.eotyExpanded is a boolean.

With regards to the second argument of setAttribute(), the docs on MDN say:

A DOMString containing the value to assign to the attribute. Any non-string value specified is converted automatically into a string.

So I thought supplying a boolean value would be fine.

How can I suppress this error?

Thanks.

Advertisement

Answer

attribute of the element can’t be a boolean, so you would probably just turn it into string instead with

new Boolean(this.eotyExpanded).toString()

Advertisement