microsoft / microsoft/TypeScript

Feature Request: Instantiate Generic Functions within Conditional Types

Offen
#61,133 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔍 Search Terms

Instantiation Expressions, Conditional Types

✅ Viability Checklist
⭐ Suggestion

I am looking for a way to be able to instantiate a generic function / return type via conditional type.

// some method

declare function method<T>(value: T): T

// ... this is possible

type R0 = ReturnType<typeof method<number>> 

// ... but this isn't 

type Infer<Func extends <T>(value: T) => unknown, U> = ReturnType<Func<U>>

type R1 = Infer<typeof method, number>

Related Issue: https://github.com/microsoft/TypeScript/issues/22617

📃 Motivating Example

I have written a small type-level parser for one of the libraries I manage. I have support for type parameterization.

Parameterized Type

const T = Syntax('number') 

const Vector = Syntax({ T }, `{ x: T, y: T, z: T }`)  // const T: TObject<{
                                                      //   x: TNumber,
                                                      //   y: TNumber,
                                                      //   z: TNumber
                                                      // }>

... but would also like to support generic functions as parameters (and resolve exterior types via function call / instantiation expression)

const Vector = <T extends TSchema>(T: T) => Type.Object({ x: T, y: T, z: T })

const T = Syntax({ Vector }, `Vector<number>`)        // const T: TObject<{
                                                      //    x: TNumber,
                                                      //    y: TNumber,
                                                      //    z: TNumber
                                                      // }>
                                      

When implementing the second case, I noticed it's not actually possible to instantiate the Vector signature within the context of a conditional type expression (i.e. the parser). In this case, I have a parsed TNumber type ready to supply the generic Vector function, but no way to instantiate it.

Essentially I would need the following but in a conditional type (Reference)

const Vector = <T extends TSchema>(T: T) => Type.Object({ x: T, y: T, z: T })

// ... internal to the parser

type ParsedNumber = TNumber 

type Instanced = ReturnType<typeof Vector<ParsedNumber>> // need this via conditional type
💻 Use Cases
  1. What do you want to use this for?

Implement generic types in a type level syntax parser.

  1. What shortcomings exist with current approaches?

Cannot instantiate generic signatures within a conditional type.

  1. What workarounds are you using in the meantime?

I'm not sure there are workarounds.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem in den Beispielen beschriebenen Verhalten von bedingten Typen und Instanziierungsausdrücken und prüfe anschließend das verwandte Issue #22617. Als abgeschlossen gilt die Aufgabe, wenn generische Funktionssignaturen mit aus einem bedingten Typ bereitgestellten Typargumenten instanziiert werden können, einschließlich der gezeigten Fälle mit Infer und Vector, ohne das erzeugte JavaScript zu ändern.

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
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.