microsoft / microsoft/TypeScript

An alternative option to `keyofStringsOnly` that stringifies numeric properties and index signatures

Đang mở
#43,041 0 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
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ả

Suggestion

🔍 Search Terms

  • keyof
  • keyofStringsOnly

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Since pattern literal types like `${number}` are valid as of https://github.com/microsoft/TypeScript/pull/40598, I feel like there should be an option to make:

type ArrayLikeKeys = keyof ArrayLike<any>;

result in:

type ArrayLikeKeys = "length" | `${number}`;

The same should happen for any numeric property key:

interface Foo {
	1: "a";
	2: "b";
	3: "c";
}

// Currently is: : 1 | 2 | 3
// Should be: "1" | "2" | "3"
type KeyOfFoo = keyof Foo;

// Currently is: never
type StrictKeyOfFoo = (keyof Foo) & (string | symbol);

like with:

interface Foo {
	"1": "a";
	"2": "b";
	"3": "c";
}

// Is: "1" | "2" | "3"
type KeyOfFoo = keyof Foo;

📃 Motivating Example

This makes keyof and numeric index signatures match runtime behaviour.

💻 Use Cases

Currently, it’s necessary to use the strictKeyof and StrictPropertyKey helpers:

type strictKeyof<T> = keyof T extends infer K
	? (K extends number ? `${K}` : K)
	: never;

type StrictPropertyKey = string | symbol;

Relevant issues:


This will most likely depend on https://github.com/microsoft/TypeScript/pull/26797, so that treating numeric index signatures as numeric pattern literal index signatures is valid:

interface ArrayLike<T> {
	readonly [index: `${number}`]: T;
	readonly length: number;
}

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. 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 xem xét các issue và pull request được liên kết, cùng với các ví dụ về keyof, thuộc tính số và index signatures trong issue này. Xác định các hàm ý thiết kế của việc chuyển các khóa số thành chuỗi và sự phụ thuộc đã nêu vào pattern literal index signatures. Được coi là hoàn thành khi tùy chọn được đề xuất tạo ra các kết quả keyof được yêu cầu mà không thay đổi hành vi hiện có.

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
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.