microsoft / microsoft/TypeScript

Runtime parameter info (name, optional, default)

Aperta
#8,126 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: Decorators Revisit Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version:

1.8.9

Code

I'm trying to leverage decorators to create runtime contracts to check against incoming parameters.

Below is the ts code

class Foo {

  @contract
  test (a : integer, b : string) : boolean {
    return true
  }

  @contract
  testOptional (a : integer, b? : string) : boolean {
    return true
  }

  @contract
  testDefault (a : integer, b : string = 'hello') : boolean {
    return true
  }
}

The above generated the following Javascript.

var Foo = (function () {
    function Foo() {
    }
    Foo.prototype.test = function (a, b) {
        return true;
    };
    Foo.prototype.testOptional = function (a, b) {
        return true;
    };
    Foo.prototype.testDefault = function (a, b) {
        if (b === void 0) { b = 'hello'; }
        return true;
    };
    __decorate([
        contract, 
        __metadata('design:type', Function), 
        __metadata('design:paramtypes', [integer, String]), 
        __metadata('design:returntype', Boolean)
    ], Foo.prototype, "test", null);
    __decorate([
        contract, 
        __metadata('design:type', Function), 
        __metadata('design:paramtypes', [integer, String]), // same as above
        __metadata('design:returntype', Boolean)
    ], Foo.prototype, "testOptional", null);
    __decorate([
        contract, 
        __metadata('design:type', Function), 
        __metadata('design:paramtypes', [integer, String]), // same as above
        __metadata('design:returntype', Boolean)
    ], Foo.prototype, "testDefault", null);
    return Foo;
}());

Note that the three different ts functions generated the exact sets of __metadata calls. The design:paramtypes alone cannot distinguish whether a particular parameter is optional or has default values, and thus insufficient for building a runtime contract.

It would be great for TypeScript to support a full parameter info so decorators can be made more capable.

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

Inizia dall’emissione dei metadati dei decorator mostrata nel JavaScript generato, in particolare dalle chiamate a design:paramtypes e __metadata. Confronta i metadati prodotti per test, testOptional e testDefault. Il lavoro è completato quando i decorator a runtime possono distinguere i nomi dei parametri, i parametri opzionali e i valori predefiniti, e il comportamento rilevante è coperto dai test.

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.