microsoft / microsoft/TypeScript

Alias Binding incorrectly propagates through properties on ephemeral import

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

まだ誰も着手していません。

Bug Domain: Binder Help Wanted
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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";

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

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

はじめの一歩

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

調査の方向性

Playground の例を再現し、TypeScript 4.0 と 4.1 の間で動作を比較します。require('./package.json') から読み取られるプロパティの alias binding に焦点を当ててください。ソースファイルやテストは指定されていません。この一時的な import に対する type-checker の経路を追跡し、version がエラーなしで再代入できることを示すカバレッジを追加してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

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

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