Decorators for functions

Ron Buckton Ron.Buckton at
Tue Oct 20 19:26:14 UTC 2015

I can think of numerous examples of how decorators could apply to functions, and I’ve added them to a gist<> [1] for easier consumption. It’s true that simple decorators for functions can work as simply as function calls, but this becomes a bit unwieldy if you need to compose multiple decorators on a single function.

Consider a scenario combining decorators providing runtime type information as an annotation with one that adds runtime type checking. With decorators this might be:

@paramtypes(() => [Number, Number])
@returntype(() => Number)
function add(x, y) { return x + y }

If I just use function expressions, this is feasible if a bit awkward:

const add =
    paramtypes(() => [Number, Number])(
        returntype(() => Number)(
                function (x, y) { return x + y; })))

It feels a bit developer-hostile to have to rebalance parentheses if you want to add a decorator, and there are ASI hazards if you misplace an opening paren. Also, you can no longer infer the function name “add” from the const binding.

Using `::` isn’t a great replacement either, as there are many hazards such as:

// The following means “call `decorator` with `this` bound to the function object”.
// Also, it becomes impossible to infer the function name “a” from the let binding.
let a = function() { }::decorator(x)

let b = function() { }
::some.decorator(x) // ASI hazard as `::` can be either prefix or infix.

One of the initial drivers for decorators was class methods, as there’s no expression context immediately inside the class body in which you can use either of the above scenarios. This necessitated a declarative form for decorators to allow these scenarios to exist. Having parity across class methods, classes, and functions (of all kinds) presents a more consistent story to developers. The upswing in decorator use in both TypeScript and Babel has been very positive, with libraries like Angular leveraging decorators heavily in their codebase. Since we introduced decorators into TypeScript, we’ve had a fair bit of feedback requesting support for function decorators.

I do think function decorators should wait until the Class/Property decorators proposal advances further along the standards track. Axel’s initial concerns/questions around hoisting are valid and there isn’t a clear consensus on the semantics for functions. That said, I’ve been mostly in the camp of introducing TDZ for function declarations that have decorators. Decorators are a new syntactic form and we have the opportunity to communicate this caveat with the development community by the time the feature lands. It seems easy enough to explain that:

function func() { }

Is the equivalent of:

let func = @decorator function() { }

Introducing TDZ allows to generally warn early as part of the static semantics, so developers won’t fall into a well with respect to adding a decorator to a function and not being able to quickly understand how that change affects the behavior of their code.

I’m not certain what the current plan of record is, but the best approach may be:

1.       Advance and get consensus on the Class/Property decorators proposal

2.       Draft a separate proposal for decorators on function expressions, generator function expressions, and arrows

3.       Draft a separate proposal for decorators on function declarations

Steps 1 and 2 above shouldn’t be significantly difficult and don’t necessarily introduce any major new semantics outside of the decorators themselves. Step 3 covers a thornier issue as it not only introduces the new semantics of decorators but also introduces side-effects due to hoisting.



From: es-discuss [mailto:es-discuss-bounces at] On Behalf Of Andrea Giammarchi
Sent: Tuesday, October 20, 2015 3:34 AM
To: Axel Rauschmayer <rauschma at>
Cc: es-discuss mailing list <es-discuss at>
Subject: Re: Decorators for functions

You haven't provided a single use-case example, like how are you going to decorate a function or why.

IMO if implemented it will be incompatible with non ES6 code unable to distinguish between classes and functions unless fully transpiled, making decorators less portable.

One thing I like about current state is that you can use decorators even in ES5 browsers [1]

Just my 2 cents, Regards

[1] as shown in the second example of the universal mixin module<>

On Tue, Oct 20, 2015 at 10:30 AM, Axel Rauschmayer <rauschma at<mailto:rauschma at>> wrote:<>

The decorator proposal does not include decorators for functions, because it isn’t clear how to make them work in the face of hoisting.

However, it would be great to have them. I see two possible solutions:

– A decorator on a function declaration prevents hoisting.

– Enable decorators for function expressions, arrow functions and generator function expressions.

Does either one of those make sense?


Dr. Axel Rauschmayer
axel at<mailto:axel at><>

es-discuss mailing list
es-discuss at<mailto:es-discuss at><>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list