Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 743
Gurbii Avatar asked Oct 04 '17 07:10

Gurbii


People also ask

How do I fix the Argument of type string is not assignable to parameter of type never?

The error "Argument of type is not assignable to parameter of type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to add elements to it. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .

Is not assignable to type string?

The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.

Is not assignable to parameter of type expected void >'?

The error "Argument of type 'void' is not assignable to parameter of type" occurs when we forget to return from a function and pass a void argument to the calling function. To solve the error, make sure you return a value of the expected type, before passing it to the caller. Here is an example of how the error occurs.

Is not assignable to parameter of type Headersinit undefined?

The "Type 'undefined' is not assignable to type" error occurs when a possibly undefined value is assigned to something that expects a different type. To solve the error, use the non-null assertion operator or a type guard to verify the value is of the specific type before the assignment.


2 Answers

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;
like image 97
Sajeetharan Avatar answered Sep 17 '22 13:09

Sajeetharan


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.

like image 36
Alexey Romanov Avatar answered Sep 18 '22 13:09

Alexey Romanov