打字稿中的通用铸造 [英] Generic casting in Typescript
问题描述
我试图在TypeScript中创建一个简单的通用查找函数,如下所示:
I'm trying to create a simple generic find function in TypeScript, along the lines of:
export function findFirst<T, U>(
array: T[],
predicate: (item: T) => boolean,
selector?: (item: T) => U): U {
...
}
所以,我的参数是:
- 要过滤的数组
- 用于测试每个元素的谓词
- 用于获取返回值的选择器
So, my parameters are:
- the array to filter through
- a predicate to test each element
- a selector to get the return value
我想要做的是提供一个默认选择器,即如果没有提供选择器,只返回整个值,例如:
What I want to do is provide a default selector, i.e. if no selector is provided, just return the whole value, i.e. something like:
if (typeof selector === "undefined")
selector = (x) => x;
但是,这样做(或者甚至(x)=>< U> ; x
)打破了函数的通用定义。如何在不删除通用参数的情况下实现默认选择器?
However this, (or even (x) => <U>x
) breaks the generic definition of the function. How can I achieve a default selector without removing the generic parameters?
如果我使用如下代码:
var arr = [1,2,3,4];
var even = findFirst(arr, x => x % 2 === 0);
返回第一个偶数,它推导出y的类型为 {}
,即 object
而不是数字。
看起来,因为U只能从选择器
参数中推断,在这种情况下是未定义的,U默认为 object
。
我知道我有太多的类型推理,但是有什么办法呢?
i.e. return the first even number, it infers the type of y as {}
i.e. object
instead of number.
It seems that, as U can only be inferred from the selector
parameter, which is undefined in this case, U defaults to object
.
I know I ask a bit too much of the type inference, but is there any way around this?
推荐答案
这里是完整的代码:
export function findFirst<T, U>(
array: T[],
predicate: (item: T) => boolean,
selector: (item: T) => U = (x:T)=> <U><any>x): U {
return array.filter(predicate).map(selector)[0];
}
的原因< U>< any>
:只能键入 U
键入 T
如果T是U的子类型或U是T的子类型。因为这不是可确定的,所以需要在之前转换为< any>
您可以断言< U>
Reason for <U><any>
: Type T
can be asserted to type U
only if T is a subtype of U OR U is a subtype of T. Since that is not determinable you need to convert to <any>
before you can assert to <U>
这篇关于打字稿中的通用铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!