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

Parameter decorators can't be used on class assigned to variables or constants #42784

Closed
ericmorand opened this issue Feb 12, 2021 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@ericmorand
Copy link

Consider the following code sample:

const Decorator = () => {
    return (target: object, key: string | symbol, index?: number) => {

    }
}

const Foo = class {
    constructor(@Decorator() foo: string) {

    }
}

const Foo1 = class Foo1 {
    constructor(@Decorator() foo: string) {

    }
}

class Bar {
    constructor(@Decorator() foo: string) {

    }
}

🙁 Actual behavior

TypeScript fails at compiling this code and throws the following compilation error for Foo and Foo1 classes:

error TS1206: Decorators are not valid here.

Class Bar compiles successfully.

🙂 Expected behavior

Since

class Foo {

}

and

const Foo = class {

}

are equivalent syntaxes in ES6, using parameter decorators should be allowed for these two syntaxes.

@ericmorand
Copy link
Author

To give a bit of context, allowing parameter decorators for anonymous classes would permit writing things like this in nestjs:

@Module({
    providers: [{
        provide: CatsService,
        useClass: class {
            constructor(@Inject(MyService) private readonly myService: MyServiceInterface) {
            }
        }
    }, MyService],
})
export class AppModule {
}

Instead of having to declare the class first and then assign it to useClass.

@RyanCavanaugh
Copy link
Member

Duplicate #7342

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 13, 2021
@ericmorand
Copy link
Author

Sorry about that. I close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants