Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

877
Visualizações
Angular CLI: 12.2.0 "No overload matches this call."

I'm trying to create an cart for an Angular project. I want to declare product as a Product but I am running into an error: (Error message shown on: "(product: Product) => { ..."

No overload matches this call. Overload 1 of 5, '(observer?: PartialObserver | undefined): Subscription', gave the following error. Argument of type '(product: Product) => void' is not assignable to parameter of type 'PartialObserver | undefined'. Property 'complete' is missing in type '(product: Product) => void' but required in type 'CompletionObserver'. Overload 2 of 5, '(next?: ((value: unknown) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription', gave the following error. Argument of type '(product: Product) => void' is not assignable to parameter of type '(value: unknown) => void'. Types of parameters 'product' and 'value' are incompatible. Type 'unknown' is not assignable to type 'Product'.ts(2769)

My cart.components.ts:

ngOnInit() {

this.message.getMessage().subscribe((product: Product) => {

  this.cartItems.push({
    id: 1,
    productName: "string",
    qty: 1,
    price: 1
  })

  this.cartItems.forEach(item => {
    this.cartTotal += (item.qty * item.price)
  })
})

}

I want to push the product selected from the array(when clicking add to cart on the menu page). So instead of having "id: 1, productName: "string"...", I'd have productName: product.name and productPrice: product.price ... This is my product.services.ts:

    export class ProductService {
  
  products: Product[] = [
    new Product(1, 'Product 1', 'P1 description', 100, 'image1.png'),
    new Product(2, 'Product 2', 'P2 description', 300, 'image2.jpg'),
    new Product(3, 'Product 3', 'P3 description', 50, 'image3.png'),
    new Product(4, 'Product 4', 'P4 description', 20, 'image4.jpg'),
    new Product(5, 'Product 5', 'P5 description', 400, 'image5.jpg'),
    new Product(6, 'Product 6', 'P6 description', 40, 'image6.jpg'),
  ]

  constructor() { }

  getProducts(): Product[] {
    return this.products
  }

}

And my cart-item.components.ts:

export class CartItemComponent implements OnInit {

  @Input() cartItem: any

  constructor() { }

  ngOnInit() {
  }

}

Messenger Service:

    export class MessengerService {    
subject = new Subject()    
constructor() { }    
sendMessage(product: Product) {     
this.subject.next(product)   
}    
getMessage() {     
return this.subject.asObservable()   
} 
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In the MessengerService as the Subject is instantiated with the new operator the generic type parameter needds to be added. The original subject = new Subject() should be changed to subject = new Subject<Product>(), this way when the Subject is converted to an Observable with this.subject.asObservable(), the returned type will be Observable<Product>. This way the getMessage() method has the correct return type and the subscribe method won't show any errors.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda