argument of type 'number' is not assignable to a parameter of type 'string'

23,322

Solution 1

Your method expects a string as argument, change it as

 this.actorService.getActor(params.get('name'))).subscribe(hero => this.actor = actor);

also declare a variable inside the ts as

actor : any;

Solution 2

For as far is i can see however it's a string and not a number

+params.get('name') is a number because unary + in JavaScript converts its operand to a number.

Share:
23,322
Gurbii
Author by

Gurbii

A youngster with a passion for web and design, goal driven and social. I am Gerben, a student New Media and Communications Technology, and this is just a short summary describing myself as a developer. From a very young age I was drawn to technology, whether it be a simple game boy or a high end computer. Making assignments for school I spend more time creating a beautiful video or PowerPoint, instead of putting the entire focus on the subject itself. Eventually I decided to make my passion my work, and chose for a career in IT. From the start on, the design appealed to me more then what was going on under the hood. That's why my focus has always been on what the user sees, feels, moves them. In order to achieve this I focused on web design, as well as on web graphics. But I didn't stop there, I extended my knowledge with video & audio skills. I am driven, don't give up easily, social, and once I bite into something I won't let go. I love working in teams, as it's the best way to extend each others knowledge and learn new things.

Updated on July 09, 2022

Comments

  • Gurbii
    Gurbii almost 2 years

    I'm quite new to typescript so forgive me if this is a noob question. I'm making a simple master detail application using angular cli. However i'm getting this error

    argument of type 'number' is not assignable to a parameter of type 'string'
    

    For as far is i can see however it's a string and not a number so i'm confused.

    My code: model

    export class Actor {
        lastName: string;
        profession: string;
        bio: string;
        url: string;
        imageUri: string;
        name: string;
        id: string;
        realName: string;
    }
    

    service

    import { Injectable } from '@angular/core';
    import { Actor } from '../model/actor';
    // import { ACTORS } from './mock-actors';
    import { Http } from '@angular/http';
    import 'rxjs/add/operator/toPromise';
    
    @Injectable()
    export class ActorService {
        private actorsUrl = '/app/persons.json';
        constructor(private http: Http) { }
        getActors(): Promise<Actor[]> {
            return this.http.get(this.actorsUrl)
                .toPromise().then(response => <Actor[]>response.json().personList)
                /*.toPromise().then(response => response.json().personList as Actor[])*/
                .catch(this.handleError);
        }
    
        private handleError(error: any): Promise<any> {
            console.error('Error: ', error);
            return Promise.reject(error.message);
        }
        getActor(name: string): Promise<Actor> {
            return this.getActors().then(actors => actors.find(actor => actor.name === name));
        }
    }
    

    component

    import { Component, OnInit } from '@angular/core';
    import { Actor } from '../../model/actor';
    import { ActivatedRoute, ParamMap } from '@angular/router';
    import { Location } from '@angular/common';
    import { ActorService } from '../../service/actor.service';
    import 'rxjs/add/operator/switchMap';
    
    @Component({
        /*selector: 'actor-detail',*/
        templateUrl: './detail-index.component.html'
    })
    export class ActorDetailComponent implements OnInit{
        actor: Actor;
        constructor(
            private actorService: ActorService,
            private route: ActivatedRoute,
            private location: Location
        ) {}
        ngOnInit(): void {
            this.route.paramMap.switchMap((params: ParamMap) =>
            this.actorService.getActor(+params.get('name'))) // argument of type 'number' is not assignable to a parameter of type 'string'
                .subscribe(hero => this.actor = actor);
        }
        goBack(): void {
            this.location.back();
        }
    }
    

    I also get the following error

    cannot find name 'actor' in the component file