typescript - 在 Typescript 中,检查对象是否包含接口的所有属性以及接口中未定义的其他属性的最佳方法是什么?
问题描述
例如,我想检查一个对象是否具有以下格式
interface InterA = { a: {
b: {
c: string //Always has this
// May have other properties here as well, like d: number, e: string
}
d: string //Always has this as well
}
我希望它能够识别出这个对象属于那种类型。
{ a: {
b: {
c: 'Always exists'
d: 'Exists this time
e: 42
}
d: 'Also always exists'
}
我想检查一个对象是否是该对象的实例,并返回该对象及其所有参数,而不仅仅是接口中定义的参数。我在想最好的方法是使用类型保护或对 a 进行功能测试,但无法让它们中的任何一个工作。最好的方法是什么?
解决方案
推荐阅读
- excel - 如何用vba删除一个txt文件的内容?
- javascript - 云功能中未定义的用户名?
- apache-pig - 使用问号的多重 if else
- django - 使用 nginx + gunicorn 服务在 django 部署中未显示媒体
- javascript - 函数 setGradient
- c - 使用 Inet_aton 接受命令行参数错误
- python-2.7 - Urllib2 通过代理和信任不受信任的 SSL 证书
- hyperledger-fabric - 在 Hyperledger Fabric 中,如何在部署时保持链代码(源代码)私有?
- sql-server - 如何根据条件从组内获取数据
- r - 如何在 mapdeck 中使用 set_token?