我有一个函数接收一个对象并将其与另一个对象合并。我希望能够有一个通用的 T,它代表正在接受的对象,因为 Typescript 知道输出对象包含相同的键,但也有新的键。
export interface ObjFactory<T> {
someNewProp: string
otherNewProp: string
...T
}
export interface InputObject {
inputProp: string
}
那么输出对象将表示为
{
someNewProp: string
otherNewProp: string
inputProp: string
}
有没有办法实现类似的东西?
最佳答案
你可以使用 Type Intersection如果您愿意导出 type
而不是接口(interface),则可以实现此目的。
export type ObjFactory<T> = T & {
someNewProp: string
otherNewProp: string
}
export interface InputObject {
inputProp: string
}
type
AFAIK 的唯一缺点是类和接口(interface)不能扩展它们。
关于generics - 有没有办法用 TypeScript 泛型进行传播操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42179024/