Microsoft/TypeScript

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

Open

#61.415 aperta il 13 mar 2025

Vedi su GitHub
 (0 commenti) (4 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
Experience EnhancementHelp WantedSuggestion

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

🔍 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

Guida contributor