Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 我该如何创建;“内部通用”;类型_Typescript - Fatal编程技术网

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 })
    }
})