Skip to content

Support decorator after class export  #31370

@apiel

Description

@apiel

Search Terms

export decorator class

Suggestion

Since TypeScript should be compatible with JavaScript, we should make decorator possible after the export.

tc39/proposal-decorators#69

Use Cases

Right now we have:

@Injectable()
export class CatsService {}

Should also be valid:

export @Injectable()
class CatsService {}

Examples

After searching around, it seem that @decorator should come after the export, even if I am still not completely sure, see how does Babel handle it:

import generate from '@babel/generator';

const { code } = generate({
    type: 'ExportNamedDeclaration',
    specifiers: [],
    source: null,
    declaration: {
        type: 'ClassDeclaration',
        decorators: [
            {
                type: 'Decorator',
                expression: {
                    type: 'CallExpression',
                    callee: {
                        type: 'Identifier',
                        name: 'Injectable',
                    },
                    arguments: [],
                },
            },
        ],
        id: {
            type: 'Identifier',
            name: 'CatsService',
        },
        superClass: null,
        body: {
            type: 'ClassBody',
            body: [],
        },
    },
} as any);

console.log(code);

Will output:

export @Injectable()
class CatsService {}

Apparently this seem to be correct output adopted by TC39 proposal:
babel/babel#7526

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: DecoratorsThe issue relates to the decorator syntaxRevisitAn issue worth coming back toSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions