microsoft / microsoft/TypeScript

Allow returning types from functions

Offen
#31,160 10 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.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

Not easily googleable because all the words are the same for extracting the function's return type.

export type from function, return type from function

Suggestion

Make it possible to return a type from a function.

The type doesn't need to depend on the runtime values passed to the function, only on their types. This feature is just syntactic sugar.

Desired:

const {decorated_fn, DecoratedFnParams} = decorate(fn);

declare const decorated_fn_params: DecoratedFnParams;
decorated_fn(decorated_fn_params);

Workaround:

const decorated_fn = decorate(fn);
type DecoratedFnParams = DecoratedParams<typeof fn>;

declare const decorated_fn_params: DecoratedFnParams;
decorated_fn(decorated_fn_params);

Alternative syntax, since this only seems useful for functions that run once:

import {decorated_fn, DecoratedFnParams} = decorate(fn);

(Shouldn't be a compatibility breaking change for the same reasons import ... = require() isn't).

Use Cases

My case is decorating a class with multiple generic parameters. Currently I have to do this:

interface Bar extends BaseBar {}
interface Baz extends BaseBaz {}
class Foo<Bar, Baz> extends BaseFoo {}

const DecoratedFoo = decorator(Foo);
type DecoratedFooBar = ExtractBar<DecoratedFoo>;
type DecoratedFooBaz = ExtractBaz<DecoratedFoo>;

But it could be just const {DecoratedFoo, DecoratedFooBar, DecoratedFooBaz} = decorator(Foo);

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 wird keine Quelldatei, kein Test und kein Einstiegspunkt genannt. Beginne damit, die angeforderte Destrukturierungssyntax, den Workaround und die im Issue verlinkten TypeScript Design Goals zu prüfen; für den Abschluss sind ein vereinbartes Typsystemdesign und ein entsprechendes Verhalten für die Beispiele erforderlich.

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.