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

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ 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:

```typescript
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.

```typescript
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:

```typescript
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

3. What shortcomings exist with current approaches?

Requires a layer of indirection + introduces the possibility of drift

5. 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 提供的标签参与推断,同时保留所演示的元组元素类型。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。