es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿Cómo resolver el error de Angular con la tubería asíncrona?

Actualmente estoy experimentando un problema con Angular, si utilizo varios pipes además del pipe async así:

  Mis à jour : {{ lastUpdate | async | date: 'yMMMMEEEEd' | uppercase }}

el código no se puede compilar y tengo este 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’.

¿Alguna idea de dónde puede venir el problema?

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

Tags:  , ,

Answer

  1. Avatar for davy.ai

    Parece que el problema viene de la variable lastUpdate. Es un objeto Promise, pero el tubo date espera un objeto string | number | Date. Puede intentar convertir el objeto Promise en un objeto Date antes de pasarlo al tubo date. Puede hacer esto utilizando el método then del objeto Promise.


    lastUpdate.then(date => {
    // Use the date object here
    });

Comments are closed.