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

291
Views
¿Cómo usar la extensión Global en Angular?

En una aplicación Angular 12, creé la siguiente extensión:

 declare global { interface String { toNumber(): number | null; } } Object.defineProperty(String.prototype, "toNumber", { value: function(this: string) { return Number(this) || null; } });

Cuando se usa en un componente de Angular:

 var number = stringValue.toNumber();

me sale el error:

 Property 'toNumber' does not exist on type 'string'.

¿Cuál es la mejor manera de usar tales extensiones en Angular?

¿Necesito cambiar la forma en que estoy creando la extensión?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes lograrlo de la siguiente manera:

  • En la carpeta src , cree el archivo global.d.ts para definir la firma del método de extensión, con el siguiente contenido:
 declare global { interface String { toNumber(): number | null; } } export {}; // to define this file as a module
  • Agregue la implementación del método de extensión dentro del archivo string.extension.ts en cualquier lugar de la carpeta src , con el siguiente contenido:
 Object.defineProperty(String.prototype, "toNumber", { value: function(this: string) { return Number(this) || null; } }); export {}; // to define this file as a module
  • Importe el archivo string.extension.ts en app.module.ts :
 import 'PATH_TO_YOUR_FILE/string-extension'
over 4 years ago · Santiago Trujillo 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!