microsoft / microsoft/TypeScript

Allow returning types from functions

Aperta
#31,160 10 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Non sono indicati file sorgente, test o punto di ingresso. Inizia esaminando la sintassi di destrutturazione richiesta, la soluzione alternativa e i TypeScript Design Goals collegati nell’issue; per completare il lavoro sono necessari un design del sistema di tipi concordato e un comportamento corrispondente per gli esempi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.