microsoft / microsoft/TypeScript

Type subscope for local type aliases

Offen
#41,470 3 Kommentare 15 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

type scope, type iife

Suggestion

A syntax that allows for a type subscope (like a js IIFE).

Use Cases

When constructing complex types, they are often not very readable without breaking them up into multiple type aliases. However, this means that you have to pass the type aliases through, along with their constraints, and it can become even less readable.

Examples

Basic examples of the concept / suggested syntax
// All of the below are equivalent to { p: { q: T } }

type X0<T> = {{
  type A = { q: T };
  type B = { p: A };
  type = B;
}}; 

type X1<T> = {{
  type = B;
  type A = { q: T };
  type B = { p: A };
}}; 

type X2<T> = {{
  type Q<U> = { q: U };
  type A = Q<T>;
  type B = { p: A };
  type = B;
}}; 

type X3<T> = {
  p: {{
    type A = { q: T };
    type = A;
  }};
};

type X4<T> = {{
  type = { p: { q: T } };
}};
Example of applying this to a real-world type

Consider the traditional approach in the following type:

// Bluebird-esque promisifyAll (monolithic):

type PromisifyAll<T> = {
  [K in keyof T & string as `${T}Async`]: (
    T[K] extends (...args: infer A) => void
      ? A extends [...infer B, (error: any, result?: infer R) => void]
        ? (...args: B) => Promise<R>
        : never
      : never
  )
}

This isn't very readable; it might help to extract some of it to type aliases:

// Bluebird-esque promisifyAll (refactored to type aliases):

type OrigArgs<Value> = Value extends (...args: infer A) => void ? A : never;
type SplitArgs<OrigArgs extends any[]> =
  OrigArgs extends [...infer A, (error: any, result?: infer R) => void]
    ? { args: A, result: R }
    : never;
type Func<SplitArgs extends { args: any[], result: any }> = (...args: SplitArgs["args"]) => Promise<SplitArgs["result"]>;
type PromisifyAll<T> = {
  [K in keyof T & string as `${T}Async`]: Func<SplitArgs<OrigArgs<T[K]>>>
}

That doesn't look great, pollutes the type scope, and has a bit of extends boilerplate.

With this proposal, you could write:

// Bluebird-esque promisifyAll (refactored to use type subscope):

type PromisifyAll<T> = {
  [K in keyof T & string as `${T}Async`]: {{
    type Value = T[K];
    type OrigArgs = Value extends (...args: infer A) => void ? A : never;
    type SplitArgs =
      OrigArgs extends [...infer A, (error: any, result?: infer R) => void]
        ? { args: A, result: R }
        : never;
    type Func = (...args: SplitArgs["args"]) => Promise<SplitArgs["result"]>;
    type = Func;
  }}
}

Or, a terser variant:

// Bluebird-esque promisifyAll (refactored to use type subscope, terser):

type PromisifyAll<T> = {
  [K in keyof T & string as `${T}Async`]: {{
    type OrigArgs = T[K] extends (...args: infer A) => void ? A : never;
    type SplitArgs =
      OrigArgs extends [...infer A, (error: any, result?: infer R) => void]
        ? { args: A, result: R }
        : never;
    type = (...args: SplitArgs["args"]) => Promise<SplitArgs["result"]>;
  }}
}

While we're all used to nested conditional types, if you saw the equivalent of the original type definition in runtime code, you would likely agree that it should be refactored.

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

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

Es werden keine Implementierungsdateien oder Tests genannt. Beginne damit, die vorgeschlagene type-subscope-Syntax und die Beispiele zu überprüfen, und verfolge anschließend die für das Parsen und Auflösen von Typaliasen zuständigen Compilerbereiche; abgeschlossen ist die Aufgabe, wenn eine abgestimmte Syntax und ein entsprechendes Verhalten des Typsystems mit Tests vorliegen.

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

Neue Issues direkt in Ihr Postfach

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