Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error TS2741: Property is missing in type

In file models/User.model.ts (see below) I tried to set a method isEqual as shown in code below. Could someone correct my code ?

file models/User.model.ts:

export class User {
    constructor(
    public firstName: string,
    public lastName: string,
    ) {}

    isEqual (other : User): boolean {
    return other === this;
    }
}

file services/user.service.ts :

import { User } from '../models/User.model';
import { Subject } from 'rxjs/Subject';

export class UserService {

    private users: User[] = [
    {
        firstName: 'William',
        lastName: 'Jones'
    },
    {
        firstName: 'John',
        lastName: 'Doe'
    }
    ];

...

ERROR in services/user.service.ts(7,2): error TS2741: Property 'isEqual' is missing in type '{ firstName: string; lastName: string;}' but required in type 'User'.

like image 835
Emile Achadde Avatar asked Jun 17 '19 07:06

Emile Achadde


1 Answers

ERROR in services/user.service.ts(7,2): error TS2741: Property 'isEqual' is missing in type '{ firstName: string; lastName: string;}' but required in type 'User'.

This is because in your class definition you have the function declared isEqual, but in your list of users you only have the firstName and lastName but not the associated function and the type of users is Users.

{
  firstName: 'William',
  lastName: 'Jones'
},

As mentioned in the comments you could use the new () to make a new User which should also include the function.

public exampleUser = new User("William", "Jones");

This could then in turn be used in an array of Users

public users: Users =  [ this.exampleUser ];
like image 127
Dince12 Avatar answered Sep 26 '22 16:09

Dince12