My Typescript function compiled without issue in version 3.5.3, but after updating to 3.8.3, it now throws a confusing error during compilation.
import { isNumber, toInteger, padNumber } from './math';
parse(value: string): NgbDateStruct {
if (value) {
const dateParts = value.trim().split('/');
if (dateParts.length === 3
&& isNumber(dateParts[0])
&& isNumber(dateParts[1])
&& isNumber(dateParts[2])
// force user to enter 4 digit year
&& dateParts[2].length === 4) {
return {month: toInteger(dateParts[0]), day: toInteger(dateParts[1]), year: toInteger(dateParts[2])};
}
}
return null;
}
When attempting to access the length property of dateParts[2]
, which is a string array, the compiler mistakenly infers the type as never
. This leads to the confusion as seen below:
The following error is generated during compilation:
ERROR in src/app/shared/utils/datepicker-parser-formatter.ts:17:25 - error TS2339: Property 'length' does not exist on type 'never'.
17 && dateParts[2].length === 4) {
~~~~~~
The function for isNumber
is defined as:
export function isNumber(value: any): value is number {
return !isNaN(toInteger(value));
}