microsoft / microsoft/TypeScript

Decorator helper `__esDecorate` refers to `Object`, which can be clobbered

オープン
#61,150 コメント 2 件 リアクション 0 件 担当者 1 名 GitHub で見る

@rbuckton がすでに取り組んでいます。

2025年2月12日 から。

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 Search Terms

__esDecorate decorator clobber object global

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about decorators
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250207#code/PTAEEECcCMEsBdIENIE9QGd5IHYBMBaWHUAMwHtJQB5aAKwFMBjeUWAWwAdL4G8zI5dqADkkAK454qTgwwiAdACgQEHKngALYgHNQ4jLtBbYGUDiTsGoAO7lxAG34ZxpUrCbX45Y5Fg6dBiotayDBSAAuFTAAIlpGFgVA+GobHAAFQVlIaQAROSY-Tm8qU3NyViQySRZYchwY5VUAJQZOByQmIzj6ZngY2wRNUBidB3JoJAcAFW0MBXi+geJjTWsAfXW5fKZKJF5o0AZ2BF5+aHRpmQYAZULYYrJYAA85VetdnFIHD3hlJg6GDMixYoAA3kpQFDQAA3KawPD7BgACgAlBFQNByOQHAxcODIdCiZAGPBxJASKQphgGABuQlQgC+SmZSlINXgdRI4HUOz2JWRVi05DwGNwqAANKBPrxnvAxep0bDyAiCUTPhgcQwFOMdMiRABZUmaEWgPDMfl8ACEIlR9NZAKQQNAADFsWroQABHmoPnIEoMzEoNEYmEq-gQonq+qa3E68h6w3G01MKa4vA2u2B5kOmOsCg+AC85gYNld2LR9ILCkmkErSiAA

💻 Code
// Arbitrary stand-in for Object imported from 'runtypes'.
// Anything using this name would suffice to trigger the error:
// "Object.getOwnPropertyDescriptor is not a function".
// Replacing "Object" with "globalThis.Object" in the __esDecorate
// emitted by TypeScript fixes the conflict.
class Object {
    validate(): boolean {
        return false;
    }
}

function AnyDecorator(method: any, context: any): void {
    console.log('Method decorated!');
}

class Foo {
    @AnyDecorator
    bar(): void {
        console.log('Method called!');
    }
}

const foo = new Foo();
foo.bar();
🙁 Actual behavior

TypeScript emits the decorator helper __esDecorate, which refers to Object. This is clobbered by an Object class defined in or imported by the module, which leads to the error Object.getOwnPropertyDescriptor is not a function at runtime.

🙂 Expected behavior

__esDecorate should use globalThis.Object to avoid this naming conflict. I've confirmed that modifying the compiler output to use globalThis.Object instead of plain Object fixes the conflict.

Additional information about the issue

This was discovered when I used decorators with the runtypes library, which exports a class called Object, used for validating object structures at runtime. The two don't even have to be used together in any meaningful way. They just have to be referenced in the same module.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。