一种可行的方法是使用单个条件映射类型而不是交集:
Species statistic p.value estimate null.value alternative method data.name
1: setosa 5095.097 2.316710e-10 0.7553375 0 two.sided Spearman's rank correlation rho Sepal.Length and Sepal.Width
2: versicolor 10045.855 1.183863e-04 0.5176060 0 two.sided Spearman's rank correlation rho Sepal.Length and Sepal.Width
3: virginica 11942.793 2.010675e-03 0.4265165 0 two.sided Spearman's rank correlation rho Sepal.Length and Sepal.Width
产生
type MergeFn = <K1 extends string,V1,K2 extends string,V2>(
k1: K1,v1: V1,k2: K2,v2: V2
) => { [K in K1 | K2]: K extends K1 ? V1 : V2}
当您说const test = mergeFn(
"hi",4,"there",4
)
/* const test: {
hi: number;
there: number;
} */
“无济于事”时,我想您的意思是编译器决定向您显示诸如Id<T>
之类的类型别名,而不是为您扩展它。在这种情况下,我发现处理它的方法是通过一个中间条件类型,例如:
Id<A & B>
如果我在您的原始代码上使用它:
type Expand<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
我得到相同的输出:
type MergeFn = <K1 extends string,v2: V2
) => Expand<Record<K1,V1> & Record<K2,V2>>
所以任何一种方法都应该对您有用。
好的,希望能有所帮助;祝你好运!
Link to code
本文链接:https://www.f2er.com/3061844.html