错误 TS2741:类型中缺少属性

编程入门 行业动态 更新时间:2024-10-23 10:25:35
本文介绍了错误 TS2741:类型中缺少属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-22 15:58:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   错误   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!