microsoft / microsoft/TypeScript

Ability to parametrize generic function type

Offen
#44,521 4 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Suggestion

I would like to propose something along the lines of $Call utility type in Flow so it is possible to represents the result of calling a generic function with a concrete parameters.

Alternatively could be an utility type that hoists parameters from generic function type into type itself e.g.

HoistTypeParams<<I, O>(inn:I[], f:(p:I) => O) => O[]> // => type DerivedType<I, O> = (inn:I[], f:(p:I) => O) => O[]

Or yet another alternative might be to add some way to parametrize through infer e.g. I would have expected following to work:

type $Call<T extends (args: any[]) => any, Args extends unknown[]> =
  T extends (...args:Args) => infer O ? O : never

🔍 Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ Viability Checklist

My suggestion meets these guidelines:

  • 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 feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

📃 Motivating Example

Today it seems impossible capture return type of the generic function, even when concrete type of the input is known. Both built-in ReturnType and infer based solutions turn generics into a unknowns.

Here is concrete example:

type Map = <I, O> (list:I[], f:(i:I) => O) => O[]

ReturnType<Map> // unknown[]
Map extends (...args:infer Args) => infer O ? O : never // unknown[]
Call<Map, [number[], (i:number) => string]> // unknown[]

type Call<F, Args extends unknown[]> = F extends (...args:Args) => infer O ? O : never

As per https://github.com/microsoft/TypeScript/issues/33185#issuecomment-526969274 it is possible to accomplish this at the implementation level, however I do not believe it is possible to do it at the type level.

💻 Use Cases

Libraries that generate more complex interfaces from a simple ones, something like RPC client for a service with generics becomes impossible here to express:

type Service<T, Path extends PropertyKey[]=[]> = {
  [K in keyof T]: T[K] extends (...args:infer I) => infer O ? Method<I, O, [...Path, K]> : Service<T[K], [...Path, K]>
}

interface Method<I extends unknown[], O, Path extends PropertyKey[]> {
  <Args extends I> (...args:Args): Promise<O>
  meta: {  path: Path }
}


declare function service <T>(impl:T):Service<T>
const s = service({
  map <I, O>(inn:I[], f:(inn:I) => O):O[] {
    return inn.map(f)
  }
})

s.map([1, 2, 3], (n:number):string => String(number))
//               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Argument of type '(n: number) => string' is not assignable to parameter of type '(inn: unknown) => unknown'.
//  Types of parameters 'n' and 'inn' are incompatible.
//    Type 'unknown' is not assignable to type 'number'.(2345)

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 der Überprüfung der motivierenden Beispiele für ReturnType, infer und die vorgeschlagene Call-Utility sowie des referenzierten Issue #33185. Definiere, wie die Parameter einer generischen Funktion konkret werden, und verifiziere, dass die Map- und Service-Beispiele die vorgesehenen Rückgabetypen erzeugen, ohne das Laufzeitverhalten 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
25/100

Neue Issues direkt in Ihr Postfach

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