使用 OR 运算符输入具有两个接口的属性时出错

发布于 2025-01-21 01:12:52 字数 870 浏览 2 评论 0原文

我有这个 ProfessionalForm 界面。 角色类型可以是 RecruiterForm 或 EmployeeForm Interface。 输入图片这里的描述

你能向我解释一下为什么这段代码会破坏我的角度应用程序吗? 输入图片此处描述

这是我的 RecruiterForm 接口:

export interface RecruiterForm {
    companyName: string;
    employeesCount: number;
}

和我的 EmployeeForm 接口:

export interface EmployeeForm {
    specialization: string;
    skills: string[];
    qualification: string;
    expectedSalary: number;
    //experiences
}

最后,这是我对相应角色的组件调用

方案可以是使用任何类型,但根本不明确。

感谢您的帮助

I have this ProfessionnalForm interface.
The type of role can be either RecruiterForm or EmployeeForm Interface.
enter image description here

Can you explain to me why this code break my angular app?
enter image description here

This is my RecruiterForm Interface:

export interface RecruiterForm {
    companyName: string;
    employeesCount: number;
}

and my EmployeeForm Interface:

export interface EmployeeForm {
    specialization: string;
    skills: string[];
    qualification: string;
    expectedSalary: number;
    //experiences
}

Finaly this is my component call with the respective roles
enter image description here

The solution can be to use any type but its not explicit at all.

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难得心□动 2025-01-28 01:12:53

问题在于您的App-Recruiter component's value属性属性recruiterform,并且您正在professional professional < /code>组件(<代码> value?。员工form

您有两个选项:

  • 扩展app-recruiter value type of Type retuiterform |员工Form
  • 确保您仅将变量传递到value类型recruiterform的属性

如果满足您的需求,则第一个选项很简单。

可以通过过滤value?。 www.typescriptlang.org/docs/handbook/advanced-types.html“ rel =“ nofollow noreferrer”> type Guard 例如)。另外,您可以重新评估value的方案?role是类型雇员,您可能会在逻辑中找到一个缺陷,这说明了为什么还可以通过此表单App-Recruiter错误地。

The problem is that your app-recruiter component's value property is of type RecruiterForm and you are passing in a variable in your professional component (value?.role) which is of type RecruiterForm | EmployeeForm

You have two options:

  • Widen the app-recruiter value variable to be of type RecruiterForm | EmployeeForm
  • Ensure that you only pass a variable into the value property that is of type RecruiterForm

The first option is straightforward if it meets your needs.

The second option can be achieved by filtering value?.role into another variable which is strictly only ever type RecruiterForm (with something like a type guard for example). Alternatively you could reassess the scenario in which value?.role is of type EmployeeForm and you may find a flaw in your logic which explains why this form may also have been being passed into app-recruiter erroneously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文