microsoft / microsoft/TypeScript

Alias Binding incorrectly propagates through properties on ephemeral import

Aperta
#43,891 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Binder Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Bug Report

This is related to the [closed] issue #41628. We have some code that requires JSON (with "resolveJsonModule": true set in our tsconfig.json) and immediately accesses a string field (package version) on the returned JSON object, assigning it to a var/let variable. This produces the following error:

Cannot assign to 'version' because it is not a variable. ts(2539)
🔎 Search Terms

require json cannot assign let alias

🕗 Version & Regression Information
  • This changed between versions 4.0 and 4.1
⏯ Playground Link

Playground link with relevant code

💻 Code
let version = require('./package.json').version;
version += "-debug";
🙁 Actual behavior

TypeScript reports the following error:

Cannot assign to 'version' because it is not a variable.
🙂 Expected behavior

We expect no error in this case. The version property on the object returned by the require function should not be considered an alias, but a simple string.

One workaround is, of course, to use the //ts-ignore hammer. Another is to do something like the following:

const pkg = require('./package.json');
let version = pkg.version;
version += "-debug";

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproducete l’esempio di Playground e confrontate il comportamento tra TypeScript 4.0 e 4.1, concentrandovi sull’alias binding per una proprietà letta da require('./package.json'). Non viene indicato alcun file sorgente o test; tracciate il percorso del type-checker per questo import effimero e aggiungete una copertura che dimostri che version può essere riassegnata senza l’errore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.