【发布时间】:2021-05-27 12:54:54
【问题描述】:
我正在将我的代码从 JS 迁移到 TS。
现在,我的组件接受对象数组,每个对象都依赖于键
interface a {
name?: string
lastName?: string
}
interface b {
email?: string
phone?: number
}
const options:a | b = {email: 'itzrahulpatel@outlook.com'}
const {
email,
phone,
name,
lastName
} = options
Typescript 在这里抛出一个错误,说(比如在电子邮件中)这样的事情
Property 'name' does not exist on type 'b'
Property 'lastName' does not exist on type 'b'
知道如何修复此类错误吗?我稍后在我的代码中处理了未定义的事情(这不是实际代码,只是一个示例代码)。
【问题讨论】:
-
更多关于
{...something}sn-p的细节 -
@C-lioGarcia 完成
-
您能否将您的解决方案发布给可能面临同样问题的人?
-
const options: a & b = {email: 'itzrahulpatel@outlook.com'}成功了
标签: typescript