microsoft / microsoft/TypeScript

Convert named tuple into object literal type.

Đang mở
#41,594 9 bình luận 46 reaction 0 người được giao Xem trên GitHub

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

Awaiting More Feedback Domain: LS: Refactorings Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

Search Terms

refactor

Suggestion

Provide a refactor that convert named tuple into object literal type.

Use Cases

Now we have named tuple.
It's common to write a tuple to pass multiple value.
As time goes on, you may add many other fields into tuple.
You cannot take a subsequence in the middle or the end of the tuple.
If we could convert that into an object, That would be useful.

Examples

type Tuple = [number, number]

function aggregate(items: number[]): Tuple {
  // ...
  return [min, max]
}

const [ min, max ] = aggregate(items)

Growth to

type Tuple = [number, number, number, number, number, number]

function aggregate(items: number[]): Tuple {
  // ...
  return [min, max, avg, mid, first, last]
}

const [ min, max, avg, mid, first, last ] = aggregate(items)

If we want first and last only


const [ , , , , first, last ] = aggregate(items) // too many '.'

const result = aggregate(items);
const first = result[4] // magic number
const last = result[5]  // magic number

If we have the refactor

// add names into tuple
type Tuple = [min: number, max: number, avg: number, mid: number, first: number, last: number]

// apply refactor
type Tuple = {min: number, max: number, avg: number, mid: number, first: number, last: number}

function aggregate(items: number[]): Tuple {
  // ...
  return {min, max, avg, mid, first, last}
}

const { first, last } = aggregate(items)

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

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 hạ tầng refactor của TypeScript language-service và các test refactor hiện có, sau đó so sánh các quy ước của chúng với các ví dụ named tuple trong issue này. Công việc được xem là hoàn tất khi một refactor có thể chuyển đổi một kiểu named tuple thành một kiểu object literal tương đương và hỗ trợ cách sử dụng dựa trên thuộc tính được mô tả, với các test bao quát việc chuyển đổ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
developer-experience
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
32/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.