microsoft / microsoft/TypeScript

Alias Binding incorrectly propagates through properties on ephemeral import

未关闭
#43,891 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Domain: Binder Help Wanted
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

复现 Playground 示例,并比较 TypeScript 4.0 和 4.1 之间的行为,重点关注从 require('./package.json') 读取的属性的别名绑定。没有指定源文件或测试;跟踪此临时导入的 type-checker 路径,并添加覆盖率以显示 version 可以在不产生该错误的情况下重新赋值。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。