1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Why is typescript declaring my parameter as undefined?

Discussão em 'Angular' iniciado por koque, Novembro 8, 2024 às 12:22.

  1. koque

    koque Guest

    In the following function I am trying to call the ProductServices's getSelectProduct method.

    private getSelectedProduct = () => {
    const productId = this.activatedRoute.snapshot.paramMap.get('productId');
    console.log('selectedProduct', productId)
    this.productService.getSelectedProduct(productId).subscribe(product => {
    this.selectedProduct = product;
    })
    }


    Typescript is giving me a red squiggly under the productId in this statement: getSelectedProduct(productId). The error message is:


    Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'.ts(2345) const productId: string | null

    This is the declaration of selectedProduct: public selectedProduct?:productModel;

    This is the getSelectedProduct method:

    public getSelectedProduct = (productId:string) => {
    const product = this.productSignal().products.find(product => product._id === productId)
    return of(product)
    }

    Continue reading...

Compartilhe esta Página