Using decorators on class reports class is declared but never used
See original GitHub issueBug Report
🔎 Search Terms
“decorator unused” “decorator declared but never used”
🕗 Version & Regression Information
- I was unable to test this on prior versions because my project relies on auto-accessor which is only available since ts 4.9
⏯ Playground Link
Playground link with relevant code
💻 Code
function defineElement(name: string): any {
}
@defineElement("my-element")
class MyElement extends HTMLElement {
}
🙁 Actual behavior
TypeScript reports 'MyElement' is declared but never used.
.
And If I click “Quick Fix” in vscode, it removes my entire class declaration and decorator call.
🙂 Expected behavior
No error reports.
Issue Analytics
- State:
- Created 9 months ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Why `Error:(3, 1) TS1238:Unable to resolve signature of class ...
Here is how class and property decorators are declared in lib.d.ts : declare type ClassDecorator = <TFunction extends Function>( ...
Read more >Primer on Python Decorators
In this introductory tutorial, we'll look at what Python decorators are and how to create and use them.
Read more >Documentation - Decorators - TypeScript
A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use...
Read more >Decorators do not work as you might expect
The claim was that decorators are not instance-targeted and only called once per class and usage. To proof this, we'll implement our own ......
Read more >Python: Decorators in OOP - Towards Data Science
We'll see three popular decorators used in classes and their ... (using the get_percent method) and generates a report as a string with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s admittedly kind of a gray area, the class is only “used” in the same sense that
let x = 0; x++;
“uses”x
. Having decorators with non-local (to the class) side effects feels like an anti-pattern. You don’t “decorate” a Christmas tree by adding more furniture to the room around it…I think it would be much better to either ignore the error, export the class or use it in an empty dummy function than to make this not an error. I believe in the majority of cases this legitimately considered unused.