microsoft / microsoft/TypeScript
Allow tuple labels passed to `satisfies` to participate in type inference
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
🔍 Search Terms
tuple, tuples, label, labels, tuple labels, satisfies, as const
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- 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:
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:
💻 Use Cases
- What do you want to use this for?
Adding free semantics
- What shortcomings exist with current approaches?
Requires a layer of indirection + introduces the possibility of drift
- What workarounds are you using in the meantime?
Outlined in the example I shared
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem verlinkten TypeScript Playground und reproduzieren Sie die Beispiele zu satisfies, as const und labeled tuples. Das Issue nennt keine Quelldateien oder Tests; abgeschlossen ist es, wenn über satisfies bereitgestellte Labels an der Inferenz teilnehmen und dabei die demonstrierten Tupel-Elementtypen erhalten bleiben.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100