Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 932
James Barrett Avatar asked Aug 22 '19 10:08

James Barrett


People also ask

How do I fix the Argument of type string is not assignable to parameter of type never?

The error "Argument of type is not assignable to parameter of type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to add elements to it. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .

Is not assignable to parameter of type Boolean?

The "Type 'boolean | undefined' is not assignable to type boolean" error occurs when a possibly undefined value is assigned to something that expects a boolean . To solve the error, use the non-null assertion operator or a type guard to verify the value is a boolean before the assignment.

Is not assignable to parameter of type string?

The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.


1 Answers

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()

like image 103
Krzysztof Krzeszewski Avatar answered Nov 01 '22 14:11

Krzysztof Krzeszewski