Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorators not allowed classes expressions #7342

Open
DavidKDeutsch opened this Issue Mar 2, 2016 · 13 comments

Comments

Projects
None yet
@DavidKDeutsch
Copy link

DavidKDeutsch commented Mar 2, 2016

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;
    }
}();
@mhegazy

This comment has been minimized.

Copy link
Contributor

mhegazy commented Mar 2, 2016

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

@mhegazy mhegazy added the Suggestion label Mar 2, 2016

@mhegazy mhegazy added this to the TypeScript 2.0 milestone Mar 2, 2016

@mhegazy mhegazy added the Committed label Mar 2, 2016

@mhegazy mhegazy changed the title Parameter decorators not allowed on methods of anonymous classes Decorators not allowed classes expressions Apr 28, 2016

@mhegazy mhegazy modified the milestones: Future, TypeScript 2.1 Sep 21, 2016

@dcworldwide

This comment has been minimized.

Copy link

dcworldwide commented 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?

@selsamman

This comment has been minimized.

Copy link

selsamman commented Mar 10, 2017

Not being able to use decorators with the mixin pattern for us means not being able to use the new mixin pattern at all. Is there a new milestone for this?

@43081j

This comment has been minimized.

Copy link

43081j commented Apr 27, 2017

any movement on this? we're still unable to use the new mixin pattern because of this.

it seems fixable by naming the class but should probably work on anonymous classes too...

@selsamman

This comment has been minimized.

Copy link

selsamman commented Apr 27, 2017

If you follow the link above #14607, it seems there is a simple work around. The whole problem seems to be that you can't return directly a class with decorators but if you define the class with a local name and then return it everything is good. We have been using that pattern and taking advantage of mixins with decorators.

@vojtechhabarta

This comment has been minimized.

Copy link

vojtechhabarta commented Apr 27, 2017

@apexskier

This comment has been minimized.

Copy link

apexskier commented May 23, 2017

Just figured out this workaround as well. Here's a simple repro showing where it works and doesn't.

function testDecorator(): PropertyDecorator {
    return function internal() {
        console.log("testing");
    };
}

class Works {
    @testDecorator()
    method(): string {
        return "works";
    }
}

function makeClassWorks() {
    class Generated {
        @testDecorator()
        method(): string {
            return "works";
        }
    }
    return Generated;
}

function makeClassDoesntWork() {
    return class {
        @testDecorator()
        method(): string {
            return "doesn't work";
        }
    };
}
tsc --experimentalDecorators ./test.ts 
narwhal/src/test.ts(26,9): error TS1206: Decorators are not valid here.
@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Jul 5, 2017

I just ran into this. I see it is a future milestone. Any chance it can get scheduled?

@rjamesnw

This comment has been minimized.

Copy link

rjamesnw commented Jul 14, 2017

I just ran into this also.

@DanielRosenwasser

This comment has been minimized.

Copy link
Member

DanielRosenwasser commented Aug 3, 2017

My prediction is that we'll tackle this once decorators reach stage 3.

@navels

This comment has been minimized.

Copy link

navels commented Aug 30, 2017

I'd love to get this sooner. We are trying out decorators to write our (Jasmine) unit tests, something like this:

@test.describe
class Foo {
  @test.it
  private someTest() { . . . }
}

but we can't do nested describes.

@shaunc

This comment has been minimized.

Copy link

shaunc commented Nov 17, 2017

Hmm... for me the workaround doesn't seem to work, when I export a function:

export function foo() {
  @decorator
  class A { .. }
  return A

Gives:

error TS4060: Return type of exported function has or is using private name

@vojtechhabarta

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.