[英]How to change the type of an object property on an extended interface?
例子:
interface Foo {
myProp: string
myObjectProp: {
objProp1: string
objProp2: number
}
}
interface Bar extends Foo {
myObjectProp: {
objProp2: string // Error: Interface 'Bar' incorrectly extends interface 'Foo'
}
}
如何更改objProp2
的类型,而不必在扩展接口上完全重新声明myObjectProp
?
您可以使用具有默认值的泛型类型。
interface Foo<T = ShapeA> {
myProp: string
myObjectProp: T
}
interface ShapeA {
objProp1: string
objProp2: number
}
interface ShapeB {
objProp2: string
}
interface Bar extends Foo<ShapeB> {
myObjectProp: {
objProp2: string
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.