Search code examples
angulartypescriptmethodsangular7

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'.


Solution

  • It is specifying that the parameter you are taking in function isEqual should be of type or model as { firstName: 'John', lastName: 'Doe' }, this shows you just need to pass data in this format.