microsoft / microsoft/TypeScript
Add a compiler option to error on destructuring a string as an array
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
### 🔍 Search Terms
string destructuring
### ✅ Viability Checklist
- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
### ⭐ Suggestion
```json
{
"compilerOptions": {
"allowDestructureStringAsArray": false
}
}
```
```typescript
declare const source: string
const [item] = source
// ^^^^^ Error: destructuring string as array is not allowed
```
### 📃 Motivating Example
The codebase is everchanging. Someone might do a refactor that change an expression from returning an array of strings to returning a single string. TypeScript which was supposed to catch mistakes like this would fail.
```diff
-return Object.entries(myObject)
+return Object.keys()
.sort(([k1], [k2]) => k1.localeCompare(k2))
```
As you can see in the above example, the developer forgot to also update the `.sort` line, but TypeScript didn't alert it. This mistake is easy to catch in small code, but real code is much bigger and similar mistake would be harder to detect.
### 💻 Use Cases
1. What do you want to use this for? Catching mistake during refactoring or code editing.
2. What shortcomings exist with current approaches? There is no "current approaches".
3. What workarounds are you using in the meantime? There is no workaround, eslint doesn't have such rule, and I doubt it cares about TypeScript's types.
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
Issue không nêu tên tệp, bài kiểm thử hoặc các điểm vào của trình biên dịch. Hãy bắt đầu bằng cách lần theo cách các tùy chọn của trình biên dịch được định nghĩa và cách destructuring mảng được kiểm tra kiểu. Được xem là hoàn tất khi một tùy chọn mới có thể từ chối việc destructuring một chuỗi như một mảng, đồng thời vẫn giữ nguyên hành vi hiện có theo mặc định, với coverage cho ví dụ được đưa ra.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- 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
- 35/100