microsoft / microsoft/TypeScript

Alias Binding incorrectly propagates through properties on ephemeral import

Đang mở
#43,891 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Domain: Binder Help Wanted
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện ví dụ Playground và so sánh hành vi giữa TypeScript 4.0 và 4.1, tập trung vào alias binding cho một property được đọc từ require('./package.json'). Không có source file hoặc test nào được nêu tên; hãy lần theo đường đi của type-checker đối với import tạm thời này và thêm coverage cho thấy version có thể được gán lại mà không gặp lỗi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.