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
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[] = []; .
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.
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.
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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With