Microsoft/TypeScript

Error when accessing CSS property value using kebab case in `CSSStyleDeclaration` object

Open

#59 968 ouverte le 14 sept. 2024

Voir sur GitHub
 (1 commentaire) (0 réactions) (0 assignés)TypeScript (6 726 forks)batch import
BugDomain: lib.d.tsHelp Wanted

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

🔎 Search Terms

"CSS property value kebab case dash", "CSSStyleDeclaration", "getComputedStyle", "getPropertyValue"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "CSSStyleDeclaration", "getComputedStyle"

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-dev.20240914#code/OYUwLgwg9gtgDgVzCAJgZTATwDYgBQpQDGCMIAdmAHQBGUKmAlANoDkNAhkQNbABOUBORQBaIlGxQ+rALpA

💻 Code

getComputedStyle(document.body)['background-color']

🙁 Actual behavior

TypeScript reports following error:

Element implicitly has an 'any' type because index expression is not of type 'number'.

🙂 Expected behavior

TypeScript should not report any error.

Additional information about the issue

If I update the file node_modules/typescript/lib/lib.dom.d.ts to include background-color as follows:

interface CSSStyleDeclaration {
  // ...
  'background-color': string;
  // ...
}

the error does not occur for background-color property.

Guide contributeur