microsoft / microsoft/TypeScript

__esDecorate transpiled code uses "Symbol" even of this is shadowed by a local variable

Ouverte
#61,884 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Bug Domain: Decorators Help Wanted
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

### 🔎 Search Terms

helper symbol tslib

### 🕗 Version & Regression Information

- This changed between versions 5.1.6 and 5.2.2

Tested via playground and reading its debug output.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250617#code/GYVwdgxgLglg9mABMOcAiBTCcBOBDKXAClxgHMYw8AbAWQygAs4ATALkTzAE8AaRbGCgYAHlA5duASkQBvAL4AoRaIAOuKAOp4AzjsQBlbgFsARnGpyBCHVBwhoxGbMTzXygAIp0WXAVwqIuo4mhDaeojG3ADC4fqyiohJgrb2jjhEzoiJSbkpmgD6YBgA7kbGiAC8iMUlhibm1JkA3DnuSkA

### 💻 Code

```ts
function fooDecorator(originalMethod: any, context: any) {}

export class Symbol { constructor() { } }

@fooDecorator
export class myClass {
constructor() {
const _newSym = new Symbol();
}
}
```

### 🙁 Actual behavior

The generated js code for `myClass` contains
```ts
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
```
Which will access my local variable `Symbol` instead of `window.Symbol`/`globalThis.Symbol`.

### 🙂 Expected behavior

The generated code should access `globalThis.Symbol` in any case or perhaps only if the file contains a local variable (from `class`, `const` or `import`) with a colliding name.

### Additional information about the issue

I found this with an `import { Symbol } from "./service";` call (roughly modelled via this [Bug Workbench](https://www.typescriptlang.org/dev/bug-workbench/#code/PTAEAEDMEsBsFMB2BDAtvAXKAzvATgG7QDG8AdAC7YBQwAVHaABKrSgDKAnqgEYD2sUHWDV4ADwAOfPBVDFYybNg7d+ggN6g5fRNgp4ArsQrSAFAEpQmgL6hr1WmChwkaTKGQSJlGtFRSZKxVeATtQSDw+VFAAIjJgXEISeBiAbgdIA0RjaB1wvj4AEXhiaWQTPFNS3X0jCqwAMSycnUt1eygC4tK8culRSWlZeUVlVE4AYQUlK2otbRrDYzM20Dn5rWq9UAB9SALQAF5QRHgAd2C1C3T5+3sgA)) but this should not change the main problem.

We started to use the class name `Symbol` in our code (and public API) since 2014.

Right now we circumvent this issue by using different names inside the local file via alias names in `import`/`export`
But this is hard to understand and maintain.

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par le playground TypeScript lié et inspectez le code __esDecorate généré afin d’y rechercher la déclaration ou l’importation locale de Symbol. Suivez la manière dont le helper de métadonnées de decorator choisit la référence à Symbol, puis vérifiez que le code émis ne résout plus le binding local et gère toujours l’exemple de decorator présenté.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.