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.

Decorators not allowed classes expressions

See original GitHub issue

Not sure if this is by design or not, but the following gives a compile error of “Decorators are not valid here” with TypeScript 1.8:

let testClass = new class {
    testMethod(@myDecorator date: Date): any {
        return date;
    }
}();

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:85
  • Comments:28 (7 by maintainers)

github_iconTop GitHub Comments

45reactions
dcworldwidecommented, Feb 23, 2017

This also applies when using the new 2.2 mixin design.

I.e.


/** Any type that can construct *something*. */
type Constructor<T> = new (...args: any[]) => T;

function ViewModel<T extends Constructor<MyModel>>(Base: T) {
	return class extends Base {

                // Won't compile. Decorator not allowed here
		@observable
		IsReadOnly: boolean;

                // Won't compile. Decorator not allowed here
		@computed get SomeProp(): string[] {
			return []
		}

		constructor(...args: any[]) {
			super(...args);
			this.IsReadOnly = true
		}
	}
}

Is this something that should work?

23reactions
mhegazycommented, Mar 2, 2016

they were never enabled for class expressions. so the behavior has not changed. but they should.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Decorators - TypeScript
A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use...
Read more >
Decorators is not valid when returning a class on a function?
Decorators are not allowed in class expressions. You can easily work around this by defining the class, then returning it:
Read more >
ICS 33 Fall 2022, Notes and Examples: Decorators
We decorate a function by adding a decorator expression above it, in which we specify ... Why not assign the name function into...
Read more >
Google TypeScript Style Guide
UpperCamelCase, class / interface / type / enum / decorator / type parameters ... not needed constructor(public baz: Baz) {} // public modifier...
Read more >
How To Use Decorators in TypeScript - DigitalOcean
Unable to resolve signature of class decorator when called as an expression. Type '(target: Function) => void' is not assignable to type ...
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 Tweet

No results found

github_iconTop Related Hashnode Post

No results found