I'm attempting to merge a method for creating strongly typed strings with type predicates but encountering issues, as evidenced by the following code snippet: https://i.sstatic.net/2b9pO.png
The issue I'm facing is
TS2339: Property 'substr' does not exist on type 'never'
, which should have been rectified by the factory function:
export function isShortDateTimeWithSpaceDelimiter(
datetime: string | ShortDateTimeWithSpaceDelimiter
): datetime is ShortDateTimeWithSpaceDelimiter {
return DateTime.fromFormat(datetime, FORMAT_ISO_DATE_SPACE_DATE_TIME).isValid;
}
export function toShortDateTimeWithSpaceDelimiter(
datetime: string
): ShortDateTimeWithSpaceDelimiter {
if (isShortDateTimeWithSpaceDelimiter(datetime)) return datetime;
throw new TypeError("Not a ShortDateTimeWithSpaceDelimiter");
}
It seems that TS is assuming the return type from the factory function is never
, indicating that something is not functioning as intended ... What am I overlooking?
The type definitions are as follows:
// https://spin.atomicobject.com/2017/06/19/strongly-typed-date-string-typescript/
enum LocalDateTimeWithSpaceDelimiterBrand {}
/** Minute granularity: "yyyy-MM-dd hh:mm" */
export type ShortDateTimeWithSpaceDelimiter = string & LocalDateTimeWithSpaceDelimiterBrand;