The Wayback Machine - https://web.archive.org/web/20250331234756/https://github.com/microsoft/TypeScript/issues/29828
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

Incorrect type for ClassDecorator #29828

Open
canonic-epicure opened this issue Feb 8, 2019 · 5 comments
Open

Incorrect type for ClassDecorator #29828

canonic-epicure opened this issue Feb 8, 2019 · 5 comments
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@canonic-epicure
Copy link

The current (3.2.2) type for class decorator function seems to be incomplete:

declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;

It says that target should extend a Function, but it should be something like NewableFunction I guess, since its a class constructor and it can be passed to new.

@ajafff
Copy link
Contributor

ajafff commented Feb 8, 2019

Would that still work with abstract classes?

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 19, 2019
@jokester
Copy link

I'm afraid the issue is still there in tsc 3.4.5 .

A few ideas when writing another for myself:

  • target should be of some extend-able type, so that we can class extends target. (from what I tried, it has to be like new () => {}. currently NewableFunction is not one.)
  • it seems difficult to handle more complicated cases: a decorator can do anything

I am using this now (it assumes constructor and instances types are not changed):

type ClassDecorator2 = <C extends new(...args: any[])=>{}>(target: C) => C | void;

@MatthewEppelsheimer
Copy link

I just ran into this. Assuming (I think reasonably) that the builtin ClassDecorator type was accurate, I spent far too long thinking I had a bug in my class decorator, when there was none.

If this remains a low priority to get right, I recommend at least making the builtin type much more permissive for anyone else running into this, since decorators are far more flexible than ClassDecorator's current TFunction | void return type.

@canonic-epicure
Copy link
Author

Just ran into this again (will be every time when using class decorator). Here's the reproducible case

The workaround from previous comment works as long as there's no static methods in the class being decorated. Curios to know how this can improved.

@canonic-epicure
Copy link
Author

Also note, that application of class decorators does not generate error, even that the intention is to decorate only subclasses of Serializable.

I believe ClassDecorator should accept an optional argument with the base class that can be decorated.

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 11, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants