Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
Error angular TS2322: 'Promesa <Plato | undefined>' no se puede asignar al tipo 'Promise<Dish>'

Aprendo Angular 5 en Coursera y tengo un problema con el tema Promise. Acabo de repetir un código de lector y tengo un error TS2322. Aquí está mi archivo de servicio.

 import { Injectable } from '@angular/core'; import { Dish } from '../Shared/dish'; import { DISHES } from '../Shared/dishes'; import { of } from 'rxjs'; import { delay } from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class DishService { constructor() { } getDishes(): Promise<Dish[]> { return of(DISHES).pipe(delay(2000)).toPromise(); } getDish(id: number): Promise<Dish> { return of(DISHES.filter((dish) => (dish.id === id))[0]).pipe(delay(2000)).toPromise(); } getFeaturedDish(): Promise<Dish> { return of(DISHES.filter((dish) => dish.featured)[0]).pipe(delay(2000)).toPromise(); } }

Y el componente que usa el servicio es:

 import { Component, OnInit } from '@angular/core'; import { Dish } from '../Shared/dish'; import { DishService } from '../services/dish.service'; @Component({ selector: 'app-menu', templateUrl: './menu.component.html', styleUrls: ['./menu.component.scss'] }) export class MenuComponent implements OnInit { dishes!: Dish[]; selectedDish!: Dish; constructor(private dishService: DishService) { } ngOnInit(): void { this.dishService.getDishes() .then(dishes => this.dishes = dishes); } onSelect(dish: Dish){ this.selectedDish = dish; } }

Luego aparece el error: Escriba 'Promise<Dish | undefined>' no se puede asignar al tipo 'Promise'. Escriba 'Plato | undefined' no se puede asignar al tipo 'Plato'. El tipo 'indefinido' no se puede asignar al tipo 'Dish'.ts(2322)

Creo que algo anda mal con el archivo service.ts, por favor explícamelo.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Tus métodos getDish y getFeaturedDish afirman devolver un Promise<Dish> pero eso no es realmente una buena suposición. Si observa su implementación, está filtrando en la variedad de platos, lo que podría resultar en un plato que no coincida con su filtro. Por lo tanto, el tipo de retorno de estos métodos debería ser Promise<Dish | undefined> y no Promise<Dish> . TypeScript no sabe que se garantizará que su matriz DISHES incluya un plato que coincida con su lógica de filtro. Creo que la solución aquí es cambiar las declaraciones de su método para que se escriban correctamente y manejen posibles valores undefined .

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!