Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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;
}
like image 698
Dawid Schmidt Avatar asked Oct 17 '17 22:10

Dawid Schmidt


People also ask

Is missing following properties from type?

The error "Type is missing the following properties from type" occurs when the type we assign to a variable is missing some of the properties the actual type of the variable expects. To solve the error, make sure to specify all of the required properties on the object.

What type of element is missing in type but required?

The TypeScript error "Property is missing in type but required in type" occurs when we do not set all of the properties an object of the specified type requires. To solve the error, make sure to set all of the required properties on the object or mark the properties as optional.

Is missing in type but required in type Iprops?

The React. js error "Property is missing in type but required in type" occurs when we don't pass all of the required props to a component. To solve the error, make sure to pass all of the props the component requires, e.g. <MyComponent name="Tom" age={30} /> or mark its props as optional.

Is missing the following properties from type component <{} {} Any >'?

js error "Type is missing the following properties from type" occurs when we don't pass any props to a component, or don't pass it all of the props it requires. To solve the error, make sure to provide all of the props that are required in the component. Here is an example of how the error occurs.


2 Answers

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

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
}, 
...
like image 136
NineBerry Avatar answered Oct 13 '22 20:10

NineBerry


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)
like image 37
Frennetix Avatar answered Oct 13 '22 21:10

Frennetix