最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to solve Argument of type 'boolean' is not assignable to parameter of type 'string&

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Aug 22, 2019 at 10:41 James BarrettJames Barrett 2,8064 gold badges27 silver badges35 bronze badges 1
  • 3 You could try out Boolean.prototype.toString() – riorudo Commented Aug 22, 2019 at 10:48
Add a comment  | 

1 Answer 1

Reset to default 11

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论