microsoft / microsoft/TypeScript
Allow creating contantly typed Map which has immutable defined set of keys
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
map, const, es6
Suggestion
Currently you can create objects and arrays with the as const assertion, which tells the compiler to, for example, type ["apple", "banana", "pear"] as ["apple", "banana", "pear"] rather than as string[]. This is useful for instances where the array is constant and you know you can rely on that.
Typing objects as const is useful for constant maps/dictionaries, however the better type to use in TypeScript for maps is, of course, Map. However, you can't type Map as const, which means you can never have a constant Map that disallows set and delete and also knows whether the result of get is undefined or not, rather than always making it return the type x | undefined.
Note: I know I can just use an object for this instead of a Map. But Map offers many benefits, such as speed and iterability, that objects don't. Also, Map is more explicit about what it is doing.
Examples
Without this feature, I have to do one of the following:
const optionA: Map<string, string> = new Map([
["date", "YYYY-MM-DD"],
["datetime", "YYYY-MM-DD[T]HH:mm"],
["datetime-local", "YYYY-MM-DD[T]HH:mm"],
["month", "YYYY-MM"],
["time", "HH:mm"],
["week", "YYYY-[W]WW"]
]);
const weekFormat = optionA.get("week") as string; // Forced to use assertion and we still lose data
or
const optionB = {
date: "YYYY-MM-DD",
datetime: "YYYY-MM-DD[T]HH:mm",
"datetime-local": "YYYY-MM-DD[T]HH:mm",
month: "YYYY-MM",
time: "HH:mm",
week: "YYYY-[W]WW]
} as const; // Have to use object
const weekFormat = optionA.week;
With this feature, I could do:
const optionA = new Map([
["date", "YYYY-MM-DD"],
["datetime", "YYYY-MM-DD[T]HH:mm"],
["datetime-local", "YYYY-MM-DD[T]HH:mm"],
["month", "YYYY-MM"],
["time", "HH:mm"],
["week", "YYYY-[W]WW"]
]) as const;
const weekFormat = optionA.get("week"); // type: "YYYY-[W]WW"
optionA.delete("time"); // error
optionA.set("time", "hh:mm"); // error
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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
Xem xét hành vi as const được đề xuất trong issue đối với Map, bao gồm các thao tác readonly và kết quả get theo từng khóa. Xác định ngữ nghĩa của hệ thống kiểu và các điểm vào liên quan của trình biên dịch cùng các bài kiểm thử trước khi xác định liệu hành vi được yêu cầu có thể được triển khai nhất quán hay không.
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
- 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
- 25/100