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

Suggestion about decorators #53411

Closed
5 tasks done
weiqj opened this issue Mar 21, 2023 · 5 comments
Closed
5 tasks done

Suggestion about decorators #53411

weiqj opened this issue Mar 21, 2023 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@weiqj
Copy link

weiqj commented Mar 21, 2023

Suggestion

Relax syntax restrictions on decorations. At least do not remove parser support for decorators on interfaces.

I wouldn't mind parser-generated warnings because those can be suppressed. But if TypeScript stops parsing decorators on interfaces and interface members I will be really disappointed.

🔍 Search Terms

decorator syntax restriction interface

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

TypeScript works great as an "intermediate programming language", which is always transpiled into other languages.

Let's keep it that way! And make sure the design meets the requirements.

I would suggest TypeScript keep decorators on interfaces and interface members.

📃 Motivating Example

I am using TypeScript to transpile to Lua. And I need decorators on interfaces.

💻 Use Cases

Why can't decorators just "decorate"? It doesn't have to be meaningfully executable at all! After all, it is what meta-programming is about.

@MartinJohns
Copy link
Contributor

Why can't decorators just "decorate"?

Because that's not what they are there for. Decorators are a JavaScript feature, not a TypeScript one. JavaScript has no interfaces, so there's nothing the decorator can decorate.

A few options you have:

  • Use classes instead of interfaces.
  • Use comments instead of decorators.
  • Fork TypeScript and implement the feature yourself.

@weiqj
Copy link
Author

weiqj commented Mar 21, 2023

Isn't TypeScript supposed to be a superset of Javascript?

You should have some vision and bring some transcendental value to this great invention.

Currently, at least pre 5.0, decorators on interfaces can be correctly parsed with a warning.

I simply suggest you keep it that way because it won't break anything. And secondly, it will bring tremendous value beyond simple Javascript transpilation!

Why can't decorators just "decorate"?

Because that's not what they are there for. Decorators are a JavaScript feature, not a TypeScript one. JavaScript has no interfaces, so there's nothing the decorator can decorate.

A few options you have:

  • Use classes instead of interfaces.
  • Use comments instead of decorators.
  • Fork TypeScript and implement the feature yourself.

@weiqj
Copy link
Author

weiqj commented Mar 21, 2023

@MartinJohns,
I just checked the source code. Although I am not an expert, there is clearly a utility API canHaveIllegalDecorators.

export function canHaveIllegalDecorators(node: Node): node is HasIllegalDecorators {

So apparently TypeScript has been very thoughtful about this matter. Though it appears that there had been a code change to rename "decorators" into "illegalDecorators" for some classes, which I will need to look into details later.

@RyanCavanaugh
Copy link
Member

Duplicate #2900

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 21, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

4 participants