Microsoft/TypeScript

Alias Binding incorrectly propagates through properties on ephemeral import

Open

#43,891 建立於 2021年4月29日

在 GitHub 查看
 (0 留言) (0 反應) (0 負責人)TypeScript (48,455 star) (6,726 fork)batch import
BugDomain: BinderHelp Wanted

描述

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

貢獻者指南

Alias Binding incorrectly propagates through properties on ephemeral import · Microsoft/TypeScript#43891 | Good First Issue