简体   繁体   English

TS 2322 - 类型'{id:number。中缺少属性'id'

[英]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.

相关问题 Typescript ts2322:类型“{ id: number, name: string, paymentStatus: string }”不可分配给类型 - Typescript ts2322: Type '{ id: number, name: string, paymentStatus: string }' is not assignable to type TS2322子类中缺少属性,但是有 - TS2322 Property missing in subclass, but it's there 类型“数字”不可分配给类型“字符串”.ts(2322) - Type 'number' is not assignable to type 'string'.ts(2322) 类型“数字”不可分配给类型“字符串”。 ts(2322) - Type 'number' is not assignable to type 'string'. ts(2322) 类型 &#39;undefined&#39; 不能分配给类型 &#39;number&#39; .ts(2322) - Type 'undefined' is not assignable to type 'number' .ts(2322) 类型“编号”不可分配给类型“PersonConfig”。 ts(2322) - Type 'number' is not assignable to type 'PersonConfig'. ts(2322) 类型“字符串”不可分配给类型“数字”.ts(2322) - Type 'string' is not assignable to type 'number'.ts(2322) 通用类型错误 TS2322:类型 &#39;{ id: null; }&#39; 不可分配给类型 &#39;T&#39; - Generic Type Error TS2322: Type '{ id: null; }' is not assignable to type 'T' 错误TS2322:输入'{id:string; ''不能赋值为'ApiModelFilter <M>' - error TS2322: Type '{ id: string; }' is not assignable to type 'ApiModelFilter<M>' 类型 &#39;(activeTabIndex: number) =&gt; void&#39; 不可分配给类型 &#39;() =&gt; number&#39;.ts(2322) - Type '(activeTabIndex: number) => void' is not assignable to type '() => number'.ts(2322)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM