Skip to content

ElementCSSInlineStyle.style should not be read-only #38838

Closed

Description

TypeScript Version: Nightly

Search Terms:

DOM Element HTMLElement SVGElement inline style setter

Expected behavior:

Per https://drafts.csswg.org/cssom/#elementcssinlinestyle, the style setter is forwarded to style.cssText, and thus accepts a string.

Actual behavior:

Type error:

(property) ElementCSSInlineStyle.style: CSSStyleDeclaration
Cannot assign to 'style' because it is a read-only property.(2540)

Related Issues:

#13466, which (correctly) points to the old spec where ElementCSSInlineStyle.style was only described as readonly, without the PutForwards=cssText annotation.

This is however a case of an old spec that doesn't match a universal browser behavior.

w3c/csswg-drafts#5127 where I asked the W3C for clarification

Code

const a = document.createElement("a")

a.style = ""
Output
"use strict";
const a = document.createElement("a");
a.style = "";
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

Activity

  1. added
    Domain: lib.d.ts
    on Jun 11, 2020
  2. byterider commented on Jan 18, 2021

    @byterider

    I wonder why even MDN says "Styles should not be set by assigning a string directly to the style property (as in elt.style = "color: blue;"), since it is considered read-only, as the style attribute returns a CSSStyleDeclaration object which is also read-only."
    https://developer.mozilla.org/en-US/docs/Web/API/ElementCSSInlineStyle/style

  3. louwers commented on May 20, 2024

    @louwers

    With #53417 merged we can now add a mutable string setter.

  4. RyanCavanaugh commented on Sep 4, 2026

    @RyanCavanaugh
    Member

    a.style = "" reports TS2540 in 4.0.0-dev.20200528 and remains affected in 5.8.0-dev.20250122. It compiles without diagnostics in 5.8.0-dev.20250123 and current native TypeScript.

    This is fixed by #60987 (b886b489), which models style as a CSSStyleDeclaration getter with a string setter, consistent with CSSOM's ElementCSSInlineStyle definition.

  5. added
    Fixed
    Needs Human Review
    on Sep 4, 2026
  6. removed
    Needs Human Review
    on Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDomain: lib.d.tsFixed

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions