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

113
Visualizações
Why the use of `Function.prototype.run` inside Angular type script class is illegal

I would like to define globally new function called run inside my Angular component like so:

Function.prototype.run = function (delay: number) {
  // some content;
};

... but the compiler generates an error that the Property 'run' does not exist on type 'Function'. If I do the same code using just plain java script and compile it using node.js it compiles without any problems. Anybody knows why?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It is generally not recommended to add properties to native JavaScript objects, but if you really want to do it, just leverage TypeScript's declaration merging:

declare global {
    interface Function {
        run: (delay: number) => void;
    }
}

Function.prototype.run = function (delay: number) {
  // some content;
};

TypeScript playground

about 4 years ago · Juan Pablo Isaza Relatório

0

To make tsc leave you alone, just cast to any

(Function.prototype as any).run = function (delay: number) {
  // some content;
};

Or create a new type

type myType = Function & { run: Function };

(Function.prototype as myType).run = function (delay: number) {
  // some content;
};

You can also use the Object class to define new properties

Object.defineProperty(Function.prototype, 'run', {
  value: (delay: number) => {},
});
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