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

216
Vistas
TypeScript isNan only accepts a number

I work with WebStorm 2016.2.2, TypeScript 2.1, Node.js.

For some reason, isNan is declared as a function that only accepts a number:

declare function isNaN(number: number): boolean;

I tried to change it to any, but it looks like it doesn't influence on the TSC. I still get the same error:

Argument of type 'string' is not assignable to parameter of type 'number'

My code (simplified):

isNan("10");

How can I solve/workaround it?


Edit:

Notice that according to specification, isNan's parameter can be any type: Number.isNan()

Also: My code was simplified. I actually receive a parameter that may be either a string or a number, and if it's a string it may be either a stringy number that I would like to convert to number ("10") or a simple string ("Hello world").

I didn't want to make this question long by including my entire code, but because it caused confusion, this is my real code:

            if (typeof expectedValue === "string" && !isNaN(expectedValue)) {
                    expectedValue = +expectedValue;
                }

            if (typeof actualValue === "string" && !isNaN(ctualValue)) {
                actualValue = +actualValue;
            }

            switch (this.operator) {
                case Operator.equal:
                    return actualValue == expectedValue;
                case Operator.notEqual:
                    return actualValue === undefined || actualValue != expectedValue;
                case Operator.greaterThan:
                    return actualValue > expectedValue;
                case Operator.littleThan:
                    return actualValue < expectedValue;
                case Operator.greaterOrEqual:
                    return actualValue >= expectedValue;
                case Operator.littleOrEqual:
                    return actualValue <= expectedValue;
            }
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I advise you to implement your code differently.
The reasons:

  1. It might be short, but it's not easy to understand what's going on
  2. Using isNaN isn't the best option here: isNaN("") returns false as well

You better try to convert the value into a number and check if that's NaN or not (as @smnbbrv wrote):

if (typeof expectedValue === "string" && !Number.isNaN(Number(expectedValue))) {
    expectedValue = Number(expectedValue);
}

Edit

You can pass your value as any:

isNaN(ctualValue as any)

To bypass the compiler check.

over 4 years ago · Santiago Trujillo Denunciar

0

You should not solve it because this is how JavaScript works.

Just cast the input to number first

Number("10") // 10
Number("abc") // NaN

and then check the result with the isNan function:

isNaN(Number("abc"))
over 4 years ago · Santiago Trujillo Denunciar

0

As ironically only numbers can be NaN, you need to transform strings into numbers first.

A very simple way to do this is the unary plus operator.

So you can do a simple isNaN(+"10").

Keep in mind that thing like +"", +" " and +"\n" are 0!

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