microsoft / microsoft/TypeScript

Contextually type 'await ...' whose value is unused to 'void | PromiseLike<void>'

Đang mở
#40,162 1 bình luận 2 reaction 1 người được giao Xem trên GitHub

@rbuckton đang làm issue này rồi.

Từ ngày 3/9/2020.

Experimentation Needed 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ả

Search Terms

contextual type await void

Suggestion

When contextually typing an await whose expression is unused, we should use the contextual type of void:

// "today" assumes https://github.com/microsoft/TypeScript/pull/39817
async function foo() {
  // today: Promise<unknown>
  // proposed: Promise<void>
  await new Promise(resolve => {
    // today: (value: unknown) => void
    // proposed: (value: void) => void
    resolve();
  });
}

A statement like await new Promise(...) is commonly used to adapt a callback-based API where the result isn't value (such as Node's fs.writeFile). Once #39817 merges, the value argument for the resolve callback will no longer be optional unless it contains void. Since the result of the await expression above is unused, we should use void as the contextual type for the await, so that the contextual type for its operand becomes void | PromiseLike<void>, thus contextually typing the Promise construction.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code:
    • This will require testing to verify
  • 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.

Đánh giá

Issue này chưa được đánh giá.

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.