microsoft / microsoft/TypeScript

Runtime parameter info (name, optional, default)

Open
#8,126 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Decorators Revisit Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the decorator metadata emission shown in the generated JavaScript, especially the design:paramtypes and __metadata calls. Compare the metadata produced for test, testOptional, and testDefault. Done means runtime decorators can distinguish parameter names, optional parameters, and default values, with the relevant behavior covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.