microsoft / microsoft/TypeScript

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

Đang mở
#61,150 2 bình luận 0 reaction 1 người được giao Xem trên GitHub

@rbuckton đang làm issue này rồi.

Từ ngày 12/2/2025.

Needs Investigation
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

### 🔎 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

```ts
// 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](https://github.com/runtypes/runtypes/), 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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.