Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

282
Vistas
Typescript: Is there a type for numerable?

I would like to know if there is a type for numerable data. To me numerable is a number or a string that can be converted into a number for exemple:

const a = 1; // it's a numerable
const b = "1"; // it's a numerable since typeof +b === 'number '
const c = "zerezre" // it's not a numerable

at the beginning I was trying to create my own type

export type Numerable = number | string;

but this was not explicite and a lot of error can appear. So I was thinking of creating validator with phantom type but this is not really what I want (I guess) since I only want to defined type and not add check inside the running program.

Can someone have any idea ?

Thanks a lot

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You should be able to use the template literal string types introduced in TypeScript 4.1 to accomplish this:

type Numerable = number | `${number}`

All of your examples work as expected - see here.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda