Home > front end >  How to resolve Angular Error with async pipe?
How to resolve Angular Error with async pipe?

Time:11-18

I am currently experiencing a problem with angular, if i use a several pipe in addition to async pipe like that :

      <p>Mis à jour : {{ lastUpdate | async | date: 'yMMMMEEEEd' | uppercase }}</p>

the code can't compile and i have this error :

Error: src/app/app.component.html:5:39 - error TS2769: No overload matches this call. Overload 1 of 3, '(value: string | number | Date, format?: string | undefined, timezone?: string | undefined, locale?: string | undefined): string | null', gave the following error. Argument of type 'unknown' is not assignable to parameter of type 'string | number | Date'. Type 'unknown' is not assignable to type 'Date'. Overload 2 of 3, '(value: null | undefined, format?: string | undefined, timezone?: string | undefined, locale?: string | undefined): null', gave the following error. Argument of type 'unknown' is not assignable to parameter of type 'null | undefined'. Type 'unknown' is not assignable to type 'null'. Overload 3 of 3, '(value: string | number | Date | null | undefined, format?: string | undefined, timezone?: string | undefined, locale?: string | undefined): string | null', gave the following error. Argument of type 'unknown' is not assignable to parameter of type 'string | number | Date | null | undefined'. Type 'unknown' is not assignable to type 'Date'.

any idea where the problem may come from?

lastUpdate = new Promise((resolve, reject) => {
    const date = new Date();
    setTimeout(
        () => {
            resolve(date);
        }, 2000
    );
});

CodePudding user response:

You are creating a promise without specifying, which type the resolved value should have, therefore it is typed as unknown. However, the date pipe does not accept type unknown.

Add type Date to the promise like the following:

lastUpdate = new Promise<Date>( //...
  • Related