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'.
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 ];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With