microsoft / microsoft/TypeScript
Alias Binding incorrectly propagates through properties on ephemeral import
Chưa có ai nhận issue này.
- 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
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- 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