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.

Add Support for design-time decorators

See original GitHub issue

Now that decorators are supported in TypeScript (#2249), consider adding support for ambient/design-time-only decorators:

Ambient decorators can be an extensible way to declare properties on or associate special behavior to declarations; design time tools can leverage these associations to produce errors or produce documentation. For example:

Use cases:

  • Deprecated/Obsolete, to support warning on use of specific API’s:
interface JQuery {  
    /**  
     * A selector representing selector passed to jQuery(), if any, when creating the original set.  
     * version deprecated: 1.7, removed: 1.9  
     */  
    @@deprecated("Property is only maintained to the extent needed for supporting .live() in the jQuery Migrate plugin. It may be removed without notice in a future version.", false)  
    selector: string;  
}
  • Suppress linter warning:
@@suppressWarning("disallow-leading-underscore")   
function __init() {  
}

Proposal

Design-time (Ambient) decorators are:

  • ambient functions
  • have a special name starting with “@”
  • are not emitted in output JS code, but persisted in .d.ts outputs.

Application

  • Applying a design-time can only accept constant values. Variables would not be observable by the compiler at compile time. Here is the set of possible values:
    • string literal,
    • number literal,
    • regexp literal,
    • true keyword,
    • false keyword,
    • null keyword,
    • undefined symbol,
    • const enum members,
    • array literals of one of the previous kinds,
    • object literal with only properties with values of one of the previous kinds

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:140
  • Comments:45 (23 by maintainers)

github_iconTop GitHub Comments

6reactions
kitsonkcommented, Jan 22, 2016

@mhegazy I might not be the best person to expound upon the proposal, as I have only lived in JavaScript land for an extended period of time (and Delphi a long time ago) and some of the Reflection and Type handling that are part of C# and other languages I am not familiar with.

I guess what I am suggesting is that at design time, in these decorators, some of the internal TypeScript constructs would be available. For example, the ability to construct and return a new type, similar to the C# TypeBuilder.CreateType

But I guess from my perspective, if I had some programmatic design time access to types via these design-time decorators, I could implement language features (mixins/traits) without needing syntatical support in typescript. From a “vision” objective, I would see something like this:

function mixin(target: any, ...mixins: any[]): type {
    const typeTarget = typeof target;
    mixins.forEach((mixin) => {
        const typeMixin = typeof mixin;
        for (let t in typeMixin) {
            typeTarget[t] = typeMixin[t];
        }
    });
    return typeTarget;
}

class A {
    foo() { return false; }
}

class B {
    bar() { return 'bar'; }
}

@@mixin(A, B)
class C {
    baz() { return 1; }
}

const c = new C();
console.log(c.foo(), c.bar(), c.baz());

Of course, I could put a lot of different logic in there, but essentially I could perform design-time operations on the types. I don’t know the inner workings of the compiler, but I suspect there would be good set of semantics for safely programmatically operating on types.

4reactions
cspotcodecommented, Sep 29, 2016

Another use case is enforcing an interface for the static side of a class.

declare function @StaticImplements<T>(t: T): T;
interface Foo { prop: string; }
@@StaticImplements<Foo> class MyClass { static prop = 'hello world'; }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Decorators
Decorators provide a way to add both annotations and a meta-programming syntax for ... To enable experimental support for decorators, you must enable...
Read more >
MVVM - Maximizing the Visual Designer's Usage with ...
Arguably, a better way is to use a design-time data service to create and populate instances of the real data classes with design-time...
Read more >
Getting to Know Stencil Decorators | by Gil Fink - Medium
Decorators are functions that annotate and modify classes and properties at design time. You can just look at them as a hook to...
Read more >
Decorate your code with TypeScript decorators
Decorators introduces programmers to write the metadata annotation which will help you to introspect your code. Best use-case you find for ...
Read more >
Typescript emits no decorator metadata
Emit serialized design-time type metadata for decorators. Add support behind an experimental compiler option to emit design-type metadata ...
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