简体   繁体   中英

TS 2322 - Property 'id' is missing in type '{ id: number

New to Angular and TS. I created model with the same properties but i got error and cant find solution:

TS2322: Type '{ id: number; model: string; plate: string; deliveryDate: string; deadline: string; client: { fir...' is not assignable to type 'Car'. Property 'id' is missing in type '{ id: number; model: string; plate: string; deliveryDate: string; deadline: string; client: { fir...'. cars : Car = [

my files:

//cars-list.component.ts

import { Car } from '../models/car';
.
.
.
cars : Car = [
{
  id: 1,
  model: 'Mazda Rx7',
  plate: 'GD2121E',
  deliveryDate: '21-04-2017',
  deadline: '05-05-2016',
  client: {
    firstName: 'Jan',
    surname: 'Kowalski'
  },
  cost: 300,
  isFullyDamaged: true
}, 
...

and

//car.ts
import {Client} from './client';
export interface Car {
  id: number;
  model: string;
  plate: string;
  deliveryDate: string;
  deadline: string;
  client: Client;
  cost: number;
  isFullyDamaged: boolean;
}

You are trying to assign an array of objects to a variable of type Car. Make the variable an array of Cars.

cars : Car[] = [

For those who used like me:

var myObject = objects.filter(o => o.id === id)

Replace it by find() to return an object and not an array:

var myObject = objects.find(o => o.id === id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM