Typescript 我该如何创建;“内部通用”;类型
没有办法直接做到这一点。您需要一个额外的泛型类型来Typescript 我该如何创建;“内部通用”;类型,typescript,Typescript,没有办法直接做到这一点。您需要一个额外的泛型类型来DnsRecords,以包含每个属性的类型: type DnsRecords = { a: { component: FC<{ a: number }> toInlineValue: (data: { a: number }) => string fromInlineValue: (record: string) => { a: number } } b: { component
DnsRecords
,以包含每个属性的类型:
type DnsRecords = {
a: {
component: FC<{ a: number }>
toInlineValue: (data: { a: number }) => string
fromInlineValue: (record: string) => { a: number }
}
b: {
component: FC<string | number>
toInlineValue: (data: string | number) => string
fromInlineValue: (record: string) => string | number
}
}
type DnsRecords = {
a: {
component: FC<{ a: number }>
toInlineValue: (data: { a: number }) => string
fromInlineValue: (record: string) => { a: number }
}
b: {
component: FC<string | number>
toInlineValue: (data: string | number) => string
fromInlineValue: (record: string) => string | number
}
}
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 }
}>
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 })
}
})