Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error TS2322: Type 'number' is not assignable to type 'string'

I am trying to build an app, where I want to get my posts, at first, by user and then when a post is clicked, I want to get this particular post by id.

And finally I get the following errors:

ERROR in src/app/cars/car-detail/car-detail.component.ts(25,11): error TS2322: Type 'Observable<{ _id: string; title: string; content: string; imagePath: string; creator: string; }>' is not assignable to type 'Car[]'.

Property 'includes' is missing in type 'Observable<{ _id: string; title: string; content: string; imagePath: string; creator: string; }>'.

src/app/cars/car-detail/car-detail.component.ts(26,11): error TS2322: Type 'number' is not assignable to type 'string'.

import { Input, Component, OnInit } from '@angular/core';
import { Car } from '../car.model';
import { CarsService } from '../cars.service';
import { ActivatedRoute, Params } from '@angular/router';


@Component({
  selector: 'app-post-detail',
  templateUrl: './post-detail.component.html',
  styleUrls: ['./post-detail.component.css']
})
export class PostDetailComponent implements OnInit {
  @Input() post: Post[] = [];
  @Input() id: string;
  constructor(
    private postsService: PostsService,
    private route: ActivatedRoute
  ) { }

  ngOnInit() {
    this.route.params
      .subscribe(
        (params: Params) => {
          this.post = this.postsService.getPost(this.id);
          this.id = +params['id'];
        }
      );
  }

}

What should I do?

like image 487
Tolis Avatar asked Aug 30 '18 14:08

Tolis


People also ask

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.

How do I convert a number to a string in TypeScript?

var num = new Number(10); console. log(num. toString()); console.

Is not assignable to type string undefined?

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

What is String [] in TypeScript?

In TypeScript, the string is sequence of char values and also considered as an object. It is a type of primitive data type that is used to store text data. The string values are used between single quotation marks or double quotation marks, and also array of characters works same as a string.


2 Answers

1)

@Input() id: string is of type string, but when you do this.id = +params['id']; - + operator tries to parse params['id'] to number, so why you get error.

Set type of the @Input() id: string to number.

2)

this.post = this.postsService.getPost(this.id);

As I consider getPost returns an observable. You need to subscribe to it and then assign the result returned from the call to the post property.

this.postsService.getPost(this.id).subscribe(res => this.post = res);
like image 161
Suren Srapyan Avatar answered Sep 29 '22 22:09

Suren Srapyan


I came up with similar situation when trying out this.

error TS2322: Type 'number' is not assignable to type 'string'

Following are the error I got When was implementing a node rest api.

error TS2322: Type 'string' is not assignable to type 'number | FindOperator<number>'.

27     const product = await repository.find({ id: req.params.id });

I had to simply change variable type from id:number to id: string.

The assocaited variable which used in this function is declared in another file. The declaration type of the that particular variable (in my case it is named as 'id') is not compatible with the variable type of the line producing the error.

So go to the place of declaration and set the variable type to number/string depending on the type, then it will compatible with the rest of the functions.

like image 39
Amila Weerasinghe Avatar answered Sep 29 '22 22:09

Amila Weerasinghe