microsoft / microsoft/TypeScript
Narrow typeof x === 'object' to Record<string | number | symbol, unknown> | null | unknown[]
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ả
Search Terms
typeof object narrow record
Suggestion
When narrowing a variable with typeof x === 'object', narrow to Record<string | number | symbol, unknown> | null | unknown[] instead of object | null.
Use Cases
When testing to see if something is an object, you almost always are starting out with unknown or similarly near-top level type and trying to narrow down to a lower "useful" type. When you use the typeof x === 'object' narrowing operation, currently TypeScript over narrows to the extremely constrained {} | null type. This suggestion would change that behavior to narrow down to the much wider Record<string | number | symbol, unknown> | null | unknown[], which the user can then choose to narrow more if they like, or they can choose to work with it as-is.
Examples
declare const apple: unknown
if (typeof apple !== 'object') throw new Error() // actual: object | null; desired: Record<string|number|symbol, unknown> | null | unknown[]
// uncomment this line and comment out the lines above to see desired behavior
// declare const apple: Record<string|number|symbol, unknown> | null | unknown[]
if (apple === null) throw new Error() // actual: object; desired: Record<string|number|symbol, unknown> | unknown[]
if (Array.isArray(apple)) throw new Error() // actual: object; desired: Record<string|number|symbol, unknown>
for (const key in apple) {
// Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
// No index signature with a parameter of type 'string' was found on type '{}'.
apple[key] // actual: error; desired: unknown
}
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
Bắt đầu bằng cách tái hiện các ví dụ TypeScript của issue và so sánh việc narrowing thực tế với kết quả được yêu cầu. Tiếp đó, lần theo đường dẫn kiểm tra kiểu chịu trách nhiệm cho việc narrowing typeof object, rồi thêm hoặc cập nhật coverage cho các trường hợp null, mảng và truy cập theo chỉ mục được nêu; hoàn thành khi các ví dụ đó được narrowing thành union được yêu cầu mà không thay đổi đầu ra lúc chạy.
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
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 35/100