I think I found a false positive of typescript:S3801.
The code looks like this:
function prepareDateTimeValue(value: ValueType | number): string {
if (value instanceof Date) {
return value.toISOString();
}
if (typeof value === "string") {
const date = new Date(value);
if (isNaN(date.valueOf())) {
throw new Error("Invalid date time string: " + JSON.stringify(value));
}
return date.toISOString();
}
throwUnexpectedValueTypeError(value, "a Date object or a date time string");
}
function throwUnexpectedValueTypeError(
value: ValueType | number,
expectedTypeDescription?: string
): never {
throw new Error(
`Unexpected value type. Value has type '${getHumanReadableObjectTypeName(
value
)}'.` + (expectedTypeDescription ? ` Expected ${expectedTypeDescription}.` : "")
);
}
SonarQube reports typescript:S3801 (Refactor this function to use “return” consistently) on prepareDateTimeValue() because the last branch does not return. So it seems it doesn’t take into account that throwUnexpectedValueTypeError() never returns.