microsoft / microsoft/TypeScript
Using 'Key in keyof ArrayType' always returns an Array regardles of the value manipulation applied
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ả
TypeScript Version: 4.0.0-beta (same behaviour in version 3.5.1 v3.5.1-Play Ground)
Search Terms:
"key in keyof Array", "keyof Array"
Expected behavior:
using key in ArrayType has the same behaviour as on any object (returning an array type only if the relevant properties are unchanged)
Actual behavior:
using key in ArrayType performs actions on touple entries but the returned type allways includes all default Array properties
Extention note:
Not the point of this issue and i know there are other issues around this topic. it's just a note,
but how about possibly adding tupple functionality for ArrayLike-, Map- and Set-types? They have all the basic qualities of a tupple, and it would be awesome to use them in a similar fashion (especially inference of Map.prototype.get/set and [Symbol.iterator] inference of tupples)
Related Issues:
#39726
#34780
type-fest/issues/119 (external library, but about this this behaviour and includes adHoc fix)
Code
type Arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
type Arr2 = (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)[]
type WhoopMyArray_Faulty<Base extends ArrayLike<any>, Condition> = {
[Key in keyof Base]:
Base[Key] extends Condition
? 'Whoop it'
: Base[Key] extends Base[number] ? Base[Key] : never
}
// action is performed on array elements but return type is still a proper array
type WhoopedArray = WhoopMyArray_Faulty<Arr, 7 | 5 | 2>
type WhoopedArrayKeys = WhoopMyArray_Faulty<Arr, 7 | 5 | 2>[keyof Arr]
// related side note, but not the main pont
// no action performed. in line with 1st example, expected: (0 | 1 | 'Whoop it' | 3 | 4 | 'Whoop it' | 6 | 'Whoop it' | 8 | 9)[]
type WhoopedArray2 = WhoopMyArray_Faulty<Arr2, 7 | 5 | 2>
type WhoopedArray2Keys = WhoopMyArray_Faulty<Arr, 7 | 5 | 2>[keyof Arr]
infered types:
type WhoopedArray = [0, 1, "Whoop it", 3, 4, "Whoop it", 6, "Whoop it", 8, 9]
type WhoopedArrayKeys = 0 | "Whoop it" | 1 | 3 | 4 | 6 | 8 | 9 | 10 | (() => string) | (() => string)
| (() => 0 | "Whoop it" | 1 | 3 | 4 | 6 | 8 | 9 | undefined) | ((...items: (0 | "Whoop it" | 1 | 3 | 4 | 6 | 8 | 9)[]) => number)
| { ...; } | ... 22 more ... | (() => IterableIterator<...>)
type WhoopedArray2 = (0 | 7 | 5 | 2 | 1 | 3 | 4 | 6 | 8 | 9)[]
type WhoopedArray2Keys = 0 | "Whoop it" | 1 | 3 | 4 | 6 | 8 | 9 | 10 | (() => string) | (() => string)
| (() => 0 | "Whoop it" | 1 | 3 | 4 | 6 | 8 | 9 | undefined) | ((...items: (0 | "Whoop it" | 1 | 3 | 4 | 6 | 8 | 9)[]) => number)
| { ...; } | ... 22 more ... | (() => IterableIterator<...>)
Output
"use strict";
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"useDefineForClassFields": true,
"alwaysStrict": true,
"allowUnusedLabels": true,
"noEmitHelpers": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "Latest",
"module": "ESNext"
}
}
Playground Link: Provided
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 với liên kết TypeScript Playground được cung cấp và các ví dụ về kiểu được ánh xạ sử dụng keyof Array và các kiểu tuple. So sánh các kiểu được suy luận với hành vi mong đợi và hành vi thực tế đã nêu, sau đó xem xét các issue liên quan #39726 và #34780. Hoàn thành khi kiểu được ánh xạ giữ nguyên các phép biến đổi phần tử tuple hoặc Array theo dự định mà không bất ngờ bao gồm các thành viên Array không thay đổ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ệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Lỗi
- Độ 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