Skip to content

es2022 decorated class with static field initialization runtime errorΒ #48818

@muhammad-salem

Description

@muhammad-salem

Bug Report

πŸ”Ž Search Terms

decorated class with static field initialization runtime error

πŸ•— Version & Regression Information

Typescript version 4.6.2
Target: ES2022
Module: ES2022

  • This changed between versions 4.5.5 and 4.6.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function decorator(name: string): any {
  return () => console.log(name)
}

@decorator("break")
class BreakStatement {
    static BreakSymbol = Symbol.for('break');
    static BREAK_INSTANCE = Object.freeze(new BreakStatement(BreakStatement.BreakSymbol));
    constructor(public symbol: Symbol){}
};

πŸ™ Actual behavior

  • compile successfully and print 'break' at runtime
  • at run time

Screenshot from 2022-04-23 12-56-13

πŸ™‚ Expected behavior

no runtime error and print 'break'

execute static BREAK_INSTANCE = Object.freeze(new BreakStatement(BreakStatement.BreakSymbol));
after the decorator applied

OR
execute decorator definition in the first static initialization block

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var BreakStatement_1;
function decorator(name) {
    return () => console.log(name);
}
let BreakStatement = BreakStatement_1 = class BreakStatement {
    static {
        BreakStatement_1 = __decorate([
            decorator("break"),
            __metadata("design:paramtypes", [Object])
        ], BreakStatement);
    }
    symbol;
    static BreakSymbol = Symbol.for('break');
    static BREAK_INSTANCE = Object.freeze(new BreakStatement_1(BreakStatement_1.BreakSymbol));
    constructor(symbol) {
        this.symbol = symbol;
    }
};

Screenshot from 2022-04-23 12-57-31

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions