microsoft / microsoft/TypeScript
No way to type an object with null prototype
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ả
Currently the base type of all objects in TS seems to be {}, which looks like an empty type but actually has members inherited from the Object object. This leaves no way to express a type with null prototype.
var foo: ??? = Object.create(null);
foo.toString(); // Want a compile error here
I did accidentally discover that it's possible to type a variable as void:
var foo: void = Object.create(null);
This appears to suppress intellisense for members in VS and the playground, and it is assignable to any (Object.keys(foo) compiles), so it would seem to be what I'm looking for. However this still lets foo.toString() compile somehow - I imagine the void type is getting elevated to Object automatically.
Edit: Ryan's workaround to subtype Object and override all its members to void prevents those properties from being called as functions, but as he points out, that doesn't prevent them from being accessed.
var bar = foo.toString; // Want a compiler error here too. Won't get one with Ryan's workaround.
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 các ví dụ Object.create(null) trong TypeScript playground, sau đó theo dõi cách {}, void và các thành viên của Object được mô hình hóa. Hoàn thành khi một kiểu được hỗ trợ có thể biểu diễn một đối tượng có nguyên mẫu null và từ chối cả hai ví dụ truy cập thành viên. Issue không nêu tên tệp hoặc bài kiểm thử nào cần nhắm tớ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
- 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