question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve CSSStyleDeclaration typings

See original GitHub issue

Not every string should be allowed in properties of the CSSStyleDeclaration interface, such as display, overflow, etc. The TypeStyle project seems to have put quite some effort into this already, from which inspiration could be taken.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:15
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

30reactions
edmundmaruhncommented, May 24, 2018

Just a question: Why is the index type of CSSStyleDeclaration’s index signature number and not string?

In case you want to apply a list of styles to HTMLElement.style one (like me) could try:

let element: HTMLElement = window.document.create('div');
let styles: { [key: string]: string } = {
    display: 'inline-block',
    width: '36px',
    height: '36px'
};

for (let style in styles) {
    element.style[style] = styles[style]; // generates TS7015, because index type is not 'number'
}

My current workaround is a cast to any:

(<any>element.style)[style] = styles[style];

which is really ugly.

6reactions
HolgerJeromincommented, Jun 15, 2021

@GlenPerkins Just use: mydiv.style.setProperty('font-size', '24px') and mydiv.style.getPropertyValue('font-size')

Read more comments on GitHub >

github_iconTop Results From Across the Web

Improve CSSStyleDeclaration typings · Issue #17827 - GitHub
Just a question: Why is the index type of CSSStyleDeclaration 's index signature number and not string ? In case you want to...
Read more >
CSSStyleDeclaration - Web APIs | MDN
Chrome Edge CSSStyleDeclaration Full support. Chrome1. Toggle history Full support. Edge12. Toggle... @@iterator Full support. Chrome51. Toggle history Full support. Edge18. Toggle... cssFloat Full support. Chrome1....
Read more >
Union type of Partial<CssStyleDeclaration> and dictionary ...
First off, the current behavior of your code is because the way discriminated union type works. CSSStyleDeclaration doesn't have a index ...
Read more >
CSSStyleDeclaration Object - W3Schools
The CSSStyleDeclaration object represents a collection of CSS property-value pairs. CSSStyleDeclaration Object Properties. Property, Description. cssText, Sets ...
Read more >
TypeScript and React: Styles and CSS - fettblog.eu
They have roughly the same properties as the CSSStyleDeclaration interface. When using React typings, these properties are already typed through csstype.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found