使用 OR 运算符输入具有两个接口的属性时出错
我有这个 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.
Can you explain to me why this code break my angular app?
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
The solution can be to use any type but its not explicit at all.
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您的
App-Recruiter
component'svalue
属性属性recruiterform
,并且您正在professional
professional < /code>组件(<代码> value?。员工form
您有两个选项:
app-recruiter
value
type of Typeretuiterform |员工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'svalue
property is of typeRecruiterForm
and you are passing in a variable in yourprofessional
component (value?.role
) which is of typeRecruiterForm | EmployeeForm
You have two options:
app-recruiter
value
variable to be of typeRecruiterForm | EmployeeForm
value
property that is of typeRecruiterForm
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 typeRecruiterForm
(with something like a type guard for example). Alternatively you could reassess the scenario in whichvalue?.role
is of typeEmployeeForm
and you may find a flaw in your logic which explains why this form may also have been being passed intoapp-recruiter
erroneously.