Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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
                This is similar to "Argument type Number is not assignable to parameter type String|Function" stackoverflow.com/questions/17355704/…
– Charles Merriam
                Jun 9, 2020 at 18:56

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;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.