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.
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[] = []; .
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With