Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Class decorator to add the “identify“ class method which returns a class name

I have a task to Implement the class decorator to add the “identify“ class method which returns a class name with the information passed in the decorator.
For example:

typescript
    @identifier('example')
    class Test {}

const test = new Test();
console.log(test['identify']()); // Test-example  

The problem is that I found out documentation where is written that I can get class name only doing const x = new Class Y and use .name function, but in my case, I don't know how the class or variable where the instance will be stored. Maybe getting this information from test units could get this done but I don't know the perfect approach cuz I'm new in typescript.
This is my try of doing the function :

const identifier = (text : string) => {
 console.log(test.constructor.name)
return test.constructor.name
}  

Yes, it's barely anything, but I don't know from where to start.
Unit tests :

describe('identifier', () => {
    it('should return Test-example from identify', () => {
        @identifier('example')
        class Test {}
        const test = new Test();
        assert.strictEqual(test['identify'](), 'Test-example');
    });

    it('should return ClassA-prototype from identify', () => {
        @identifier('prototype')
        class ClassA {}
        const test = new ClassA();
        assert.strictEqual(test['identify'](), 'ClassA-prototype');
    })
});  

Could you guys please help me? Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I came up with this function that solves my issue :

function identifier(...args: any): ClassDecorator {
    return function <TFunction extends Function>(
        target: TFunction
    ): TFunction | any {
        var identify = target.prototype.identify;
        Object.defineProperty(target.prototype, 'identify', {
            value: function() {
                return target.name + "-" + args;
            }
        });
        return identify;
    };
}  

It creates the identify method for the class,pass to the method the class name and the string from parantheses and then return it.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!