Javascript 如何在TypeScript中通用地键入变量绑定?
我有一个通用接口可供我使用,例如:Javascript 如何在TypeScript中通用地键入变量绑定?,javascript,typescript,ramda.js,Javascript,Typescript,Ramda.js,我有一个通用接口可供我使用,例如: CurriedFunction3<T1, T2, T3, R>: R 泛型类型CurriedSorter需要1个类型参数 constbindingsortFactory应该是一个具有一个泛型类型参数的函数,可以这样使用: sortFactory<MyType>( prop('name'), 'DESC', [{ name: 'foo' }, { name: 'bar' }] ) // returns `[{ name: '
CurriedFunction3<T1, T2, T3, R>: R
泛型类型CurriedSorter
需要1个类型参数
const
bindingsortFactory
应该是一个具有一个泛型类型参数的函数,可以这样使用:
sortFactory<MyType>(
prop('name'),
'DESC',
[{ name: 'foo' }, { name: 'bar' }]
) // returns `[{ name: 'bar' }, { name: 'foo' }]`
sortFactory(
道具(‘名称’),
“DESC”,
[{name:'foo'},{name:'bar'}]
)//返回`[{name:'bar'},{name:'foo'}]`
如何在TypeScript中通用地键入变量绑定?使用TypeScript有什么方法可以做到这一点吗?在变量声明的类型中不能有一个空的
t
,因为t
可能会通过属性漏出值。但是,您可以将通用调用签名作为变量类型的一部分。这是一个自成一体的示例,因为我不知道您将uncurryN
,unapply
等定义为:
type CurriedFunction3<T1, T2, T3, R> = (a: T1, b: T2, c: T3) => R;
type CurriedSorter<T> = CurriedFunction3<(obj: T) => any, string, T[], T[]>;
type CurriedSorterValue = {
<T>(a: (obj: T) => any, b: string, c: T[]): T[];
}
const sortFactory: CurriedSorterValue = function (a, b, c) {
return c;
}
type CurriedFunction3=(a:T1,b:T2,c:T3)=>R;
输入CurriedSorter=CurriedFunction3 any,string,T[],T[]>;
类型CurriedSorterValue={
(a:(obj:T)=>any,b:string,c:T[]):T[];
}
const sortFactory:CurriedSorterValue=函数(a、b、c){
返回c;
}
sortFactory的类型是什么?您是否尝试在const sortFactory:CurriedSorter
中提及T
?sortFactory
应为CurriedSorter
,即接受一个传递给CurriedSorter
的泛型类型。我不确定如何在const sortFactory
中提到T
,这就是这个问题的内容。当我执行const-sortFactory:CurriedSorter
时,我收到一个错误“找不到名称“T”。您这里所说的generic-call-signature
是什么意思?这是否意味着声明一个类型,它实际上是一个具有泛型签名的函数类型?如类型CurriedSorterValue
右侧<代码>(a:(obj:T)=>任意,b:string,c:T[]):T[]是泛型调用签名(可能包含在非泛型类型中),而CurriedSorter
是泛型类型。
sortFactory<MyType>(
prop('name'),
'DESC',
[{ name: 'foo' }, { name: 'bar' }]
) // returns `[{ name: 'bar' }, { name: 'foo' }]`
type CurriedFunction3<T1, T2, T3, R> = (a: T1, b: T2, c: T3) => R;
type CurriedSorter<T> = CurriedFunction3<(obj: T) => any, string, T[], T[]>;
type CurriedSorterValue = {
<T>(a: (obj: T) => any, b: string, c: T[]): T[];
}
const sortFactory: CurriedSorterValue = function (a, b, c) {
return c;
}