why does typescript compiler think that the element in a string array would be type ‘never’?

isNumber is defined incorrectly. It’s asserting that anything that can be converted to a number is a number. Those are two different things. The correct definition would be something like:

export function isNumber(value: any): value is number {
  return typeof value === "number" && !isNaN(value);
}

…if you want something that filters out NaN. (I didn’t filter out fractionsl values.)

As @jtbandes said, since isNumber is telling TypeScript that dateParts[2] is a number, but TypeScript knows from split that it’s a string, it’s trying to apply string & number — which is never.

You’ll want an isNumeric or similar along these lines for places like this where you know it’s not a number:

export function isNumeric(value: any): boolean {
  return !isNaN(toInteger(value));
}

Note that it doesn’t make the assertion.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top