扩展对象的打字稿数组,而无需修改源
我有此列表对象,但是后来更改了形状,我需要一个额外的ID属性,但是如何不修改列表?
interface List {
name: string //without adding id here
}
interface ListData {
type: string
lists: List[] //how to include id here?
}
const data: ListData = {
type: 'something',
lists: [{
id: '1',
name: 'alice'
}, {
id: '2',
name: 'ja'
}]
}
I have this List object, but later on the shape changed, I need an extra id property, but how to not modify List?
interface List {
name: string //without adding id here
}
interface ListData {
type: string
lists: List[] //how to include id here?
}
const data: ListData = {
type: 'something',
lists: [{
id: '1',
name: 'alice'
}, {
id: '2',
name: 'ja'
}]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以 Extend
You can extend the
List
interface您可以使用
扩展
为扩展现有接口的关键字,也可以在现有
list> list>接口中创建ID可选,因此,如果您已经使用了它,则可以在没有影响任何一个的情况下使用它。
Note :
?
用于使属性可选。you can use
extends
keyword for extend existing interfaceor you can make id optional in existing
List
interface so if you already used it some where it was not affect any one.Note:
?
is used to make property optional.为以前的答案添加替代方法
,或者不漂亮,而是有效
to add alternative approach to previous answers
or, not pretty but works