没有办法直接这么做。您需要一个额外的泛型类型来
DnsRecords
要包含每个属性的类型,请执行以下操作:
type DnsRecords<T> = {
[P in keyof T]: {
component: FC<T[P]>
toInlineValue: (data: T[P]) => string
fromInlineValue: (record: string) => T[P]
}
}
type V = DnsRecords<{
a: { foo: string },
b: { bar: string }
}>
你可以创建一个函数来帮助推断
T
但如果使用不同的值,编译器将推断属性的并集:
function createDns<T>(dns: DnsRecords<T>) {
return dns;
}
type A = { x: string }
type B = { x2: string }
let o = createDns({ // T is { a: A | B; }
a: {
component: (p: A) => { },
toInlineValue: (data: B) => "",
fromInlineValue: (r) => ({ x: r })
}
})