问题描述
限时送ChatGPT账号..在文件 models/User.model.ts(见下文)中,我尝试设置一个方法 isEqual,如下面的代码所示.有人可以更正我的代码吗?
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 ?
文件模型/User.model.ts:
file models/User.model.ts:
export class User {
constructor(
public firstName: string,
public lastName: string,
) {}
isEqual (other : User): boolean {
return other === this;
}
}
文件服务/user.service.ts :
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'
}
];
...
services/user.service.ts(7,2) 中的错误:错误 TS2741:属性 'isEqual' 在类型 '{ firstName: string;lastName: string;}' 但在类型 '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'.
推荐答案
它指定您在函数 isEqual
中获取的参数的类型或模型应为 { firstName: 'John', lastName: 'Doe' }
,这说明你只需要以这种格式传递数据即可.
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.
这篇关于错误 TS2741:类型中缺少属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论