Skip to content

Typescript 5 ES Class Decorators runtime error when passing class self-static property #53778

Closed

Description

@szdolhan-abs

Bug Report

We wanted to migrate to the new native decorators when upgrading to typescript 5, but ran into problem.
I get a runtime error when I pass a static variable of the same class to the class decorator. It seems that static members are initializing after decorator execution.

🔎 Search Terms

  • decorators
  • decorators static
  • es decorators

🕗 Version & Regression Information

Typescript 5+and es decorators. ExperimentalDecorators works well with similar code.

⏯ Playground Link

Playground link with relevant code

💻 Code

function defineCustomElement(name: string) {
    return (target: CustomElementConstructor, context: ClassDecoratorContext) => {
        customElements.define(name, target);
    };
}

@defineCustomElement(CustomElement.TAGNAME)
class CustomElement extends HTMLElement{
    public static TAGNAME = "custom-tag";
}

const instance = new CustomElement();

🙁 Actual behavior

Runtime Error: Cannot read properties of undefined (reading 'TAGNAME')

🙂 Expected behavior

Run without errors.

Activity

  1. added
    Needs Investigation
    on Apr 14, 2023
  2. Jamesernator commented on Apr 14, 2023

    @Jamesernator

    I get a runtime error when I pass a static variable of the same class to the class decorator. It seems that static members are initializing after decorator execution.

    This is the correct behaviour, in the decorators proposal all class decorators are evaluated before the class is evaluated. (See 15.8.21).

    If you want this sort've behaviour you'll need to use a function that can be called after the class has been fully defined:

    function defineCustomElement(getName: () => string) {
        return function(klass: CustomElementConstructor, ctx: ClassDecoratorContext<CustomElementConstructor>) {
            ctx.addInitializer(function(this: CustomElementConstructor) {
                customElements.define(getName(), this);
            });
        };
    }
    
    @defineCustomElement(() => CustomElement.TAGNAME)
    class CustomElement extends HTMLElement{
        public static TAGNAME = "custom-tag";
    }
    
    const instance = new CustomElement();
  3. nicolo-ribaudo commented on Apr 17, 2023

    @nicolo-ribaudo
    Contributor

    An alternative would be to define the static property in the decorator:

    function defineCustomElement(name: string) {
        return (target: CustomElementConstructor, context: ClassDecoratorContext) => {
            Object.defineProperty(target, "TAGNAME", {
              configurable: true,
              writable: true,
              enumerable: true,
              value: name,
            });
            customElements.define(name, target);
        };
    }
    
    @defineCustomElement("custom-tag")
    class CustomElement extends HTMLElement{
    }
    
    const instance = new CustomElement();
  4. added
    Working as Intended
    and removed
    Needs Investigation
    on Aug 4, 2023
  5. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Working as Intended

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    ⌂ Proxy