typescript - 将接口的对象转换为其基本接口
问题描述
interface IPerson {
firstName: string;
lastName: string;
}
interface IPersonWithPhone extends IPerson {
phone: string;
}
const personWithPhone: IPersonWithPhone = {
firstName: "Foo",
lastName: "Boo",
phone: "+1 780-123-4567"
}
可以说IPersonWithPhone
延伸IPerson
。我想转换personWithPhone
成person
, 意思personWithPhone.phone = undefined
。我不想改变对象,同时我不想单独设置每个属性。像下面这样的东西,
// Not like this
const person: IPerson = {
firstName: personWithPhone.firstName,
lastName: personWithPhone.lastName
}
我正在寻找类似于 spread 的东西,它可以删除一个属性并可以转换为基本接口。
// example of spread
cont person: IPerson = {
firstName: "Foo",
lastName: "Boo",
}
const personWithPhone: IPersonWithPhone = {...person, phone: "+1 780-123-4567"};
解决方案
推荐阅读
- javascript - 在 componentDidMount() 中反应 Axios 调用
- reactjs - 将 Vue 插槽代码转换为 React 插槽代码 - 传递数据时遇到问题
- windows - 无法使用远程容器将 C/C++ 扩展安装到 VisualCode
- ios - 在 App Store Connect 上找不到编辑版本。尝试使用'--use_live_version true'
- magento - Magento 2.3.3 数据库 ERD
- azure - Azure Blob 存储错误 - 不满足使用 HTTP 条件标头指定的条件
- firebase - 颤振 | 如何从 Firestore 获取对象列表?
- performance - 如何提高 Fortran 到 Fortran 操作的效率?
- mysql - 如何检查一列是否有两个以上的项目,以及它是否比 SQL 中的 avg() 多?
- themes - 如何更改 RStudio 终端中光标的颜色?