[英]TS 2322 - Property 'id' is missing in type '{ id: number
New to Angular and TS. Angular和TS新手。 I created model with the same properties but i got error and cant find solution:
我创建了具有相同属性的模型,但我得到错误,无法找到解决方案:
TS2322: Type '{ id: number;
TS2322:输入'{id:number; model: string;
model:string; plate: string;
盘子 deliveryDate: string;
deliveryDate:string; deadline: string;
截止日期:字符串; client: { fir...' is not assignable to type 'Car'.
client:{fir ...'不能分配给'Car'类型。 Property 'id' is missing in type '{ id: number;
类型'{id:number; model: string;
model:string; plate: string;
盘子 deliveryDate: string;
deliveryDate:string; deadline: string;
截止日期:字符串; client: { fir...'.
客户:{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. 您正在尝试将对象数组分配给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: 用find()替换它以返回一个对象而不是一个数组:
var myObject = objects.find(o => o.id === id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.