microsoft / microsoft/TypeScript
Decorator helper `__esDecorate` refers to `Object`, which can be clobbered
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
### 🔎 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.
Beitragsleitfaden
Bewertung
Dieses Issue wurde noch nicht bewertet.