Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda