'Extending Angular DatePipe errors in Angular 11, worked in Angular 10

Originally, I had a simple extension to the DatePipe to transform a date value from our standard (YYYYMMDD format) so that the DatePipe in Angular could then use it.

export class SlsDatePipe extends DatePipe implements PipeTransform {
    constructor(Locale:string) {
        super(Locale);
    }

    /**
     * Convert the data from the numeric YYYYMMDD to the Javascript date format
     * @param DateValue SLS Formatted number in the YYYYMMDD style
     * @param args Angular Date Pipe args to control the output format
     * @returns Text output following the Javascript date() formats
     */
    public transform(DateValue:number, args:string):string | null {
    
        const DateString:string = DateValue.toString();
        const Year:number  = parseInt(DateString.substr(0, 4), 10);
        const Month:number = parseInt(DateString.substr(4, 2), 10) - 1;
        const Day:number   = parseInt(DateString.substr(6, 2), 10);
        const DateObject:Date = new Date(Year, Month, Day);

        return super.transform(DateObject, args);
}

There is some additional checking in the class for proper formats, etc., but the basics of the issue are there. This worked in Angular 10. I just updated to Angular 11, and the code now produces an error on the transform:

error TS2416: Property 'transform' in type 'SlsTimePipe' is not assignable to the same property in base type 'DatePipe'.
    Type '(TimeValue: number, args: string) => string' is not assignable to type '{ (value: string | number | Date, format?: string, timezone?: string, locale?: string): string; (value: null,
    format?: string, timezone?: string, locale?: string): null; (value: string | number | Date, for mat?: string, timezone?: string, locale?: string): string; }'.
    Type 'string' is not assignable to type 'null'.


Solution 1:[1]

Angular 11 brings stricter types for DatePipe where method overloading is used.

You can calm down TypeScript compiler by adding overload for transform method like:

transform(DateValue: null | undefined, args?: string): null;
transform(DateValue: number, args: string): string | null {
  ...
}

Solution 2:[2]

Yurzui's answer didn't work for me. After a fighting with this for a while I decided to tell TypeScript to frig off with any:

transform(value: string | number | Date, timezone: string = null): any {
    return super.transform(value, DATE_FORMATTING_STRING, timezone);
}

Solution 3:[3]

Overloads should be

transform(value: null | undefined, format?: string): null;
transform(
    value: Date | string | number | null | undefined,
    format?: string,
): string | null; 
transform(
    value: Date | string | number | null | undefined,
    args?: string,
): string | null {
    //
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yurzui
Solution 2 bobbyg603
Solution 3 practicalgnome