microsoft / microsoft/TypeScript

Allow tuple labels passed to `satisfies` to participate in type inference

未關閉
#61,415 0 則留言 4 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

Experience Enhancement Help Wanted Suggestion
主要語言
Go
星號
111k
分支
14.3k
平均合併
2 天 4 小時
30 天內合併 PR
132

描述

🔍 Search Terms

tuple, tuples, label, labels, tuple labels, satisfies, as const

✅ Viability Checklist
⭐ Suggestion

Hey TypeScript team! First of all, super excited to hear about tsgo.

I've got a feature request for y'all -- the body of this issue was ported from a playground that I created:

https://tsplay.dev/NljeQW

Basically, I often find myself using the satisfies operator to define mutable tuples inline:

const base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as const satisfies any[]
//    ^?  const base: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Sometimes when working with tuples of a known length, I use labels, because why not? Free semantics!

But to get the behavior I'm after, I have to add a layer of indirection.

const ex = [0, 0] as const
type Example = never | [x: typeof ex[0], y: typeof ex[1]]
const example: Example = [...ex]
//    ^? const example: [x: 0, y: 0]

This makes the code more confusing to read, which makes the tradeoff basically a wash.

Feature Request

Allow any labels passed to satisfies to participate in type inference, like so:

const point = [0, 0] as const satisfies [x: any, y: any]
// 
//     Desired type:
//     ^? const point: [x: 0, y: 0]
//                      ^     ^

Let me know if you'd like a more complex example, or if there's anything else I can provide.

📃 Motivating Example

Playground:

https://tsplay.dev/NljeQW

💻 Use Cases
  1. What do you want to use this for?

Adding free semantics

  1. What shortcomings exist with current approaches?

Requires a layer of indirection + introduces the possibility of drift

  1. What workarounds are you using in the meantime?

Outlined in the example I shared

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從連結的 TypeScript Playground 開始,重現涉及 satisfies、as const 和 labeled tuples 的範例。該 issue 未指定原始檔案或測試;完成表示透過 satisfies 提供的標籤會參與推論,同時保留所示範的 tuple 元素型別。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
typescript
領域
compilers
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。