question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Runtime parameter info (name, optional, default)

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mattvb91commented, Mar 3, 2021

Currently have the same use case and need to see if a decorated class attribute is optional or not.

1reaction
canassacommented, Jun 17, 2020

👍 for including the optionality is the metadata information. Here is my use case for it:

I am building a configuration library that reads from several sources (environment variables, AWS SSM, etc). I use the decorators to identify how to fetch the attribute. e.g.:

class Config {
    @Env DB_PASSWORD!: string
}

The @Env reads the DB_PASSWORD type using the getMetadata function and I use its type to correctly parse the environment variable. Unfortunately, the metadata does not indicate to me if a field is marked as optional or not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use runtime and type-safe parameters - Azure Pipelines
Parameters must contain a name and data type. Parameters cannot be optional. A default value needs to be assigned in your YAML file...
Read more >
Default parameters and reflection: if ParameterInfo.IsOptional ...
The short answer to my question is no - just because IsOptional is true doesn't mean that DefaultValue will actually contain the real...
Read more >
Named And Optional Parameters In C# - C# Corner
By default, all parameters of a method are required. But in C# 4.0, the concept of optional parameters was introduced that allows developers...
Read more >
Optional Runtime Parameter - Visual Studio Feedback
For example, parameters: - name: force_k8s_namespace displayName: Force K8s Namespace type: string default: '' optional: true ### <<----- ...
Read more >
Default parameters - JavaScript - MDN Web Docs
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found