Typescript推断泛型中的泛型 [英] Typescript infer generics within generics
本文介绍了Typescript推断泛型中的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个界面 Wrapped
:
interface Wrapped<T> {
data: T
}
我想定义一个这样的函数:
And I want to define a function like this:
function f<T>(arg: any): T {
const obj: Wrapped<T> = doSomethingAndGetWrappedObject<T>(arg)
return obj.data
}
// Don't pay attention to the argument, it is not important for the question
const n: number = f<number>(/* ... */)
问题是,在我的应用程序中,传递 number
作为类型参数非常不方便,我想传递 Wrapped< number>
,即调用 f
像这样:
The problem is, in my application it is very inconvenient to pass number
as type argument, I would like to pass Wrapped<number>
instead, i.e. to call f
like this:
const n: number = f<Wrapped<number>>(/* ... */)
问题是:如何键入 f
使其成为可能?
The question is: how to type f
to make it possible?
function f<T extends Wrapped<V>, V>(arg: any) {
// ...
}
// Now this works, but it is very annoying to write the second type argument
const n: number = f<Wrapped<number>, number>()
// I would like to do this, but it produces an error
// Typescript accepts either no type arguments or all of them
const n: number = f<Wrapped<number>>()
// This just works in an unpredictable way
function f<T extends Wrapped<any>>(
arg: any
): T extends Wrapped<infer V> ? V : any {
/* ... */
}
推荐答案
您可以使用 infer
关键字来创建用于提取通用类型的帮助程序类型.
You could create helper type for extracting the generic type by using infer
keyword.
interface Wrapped<T> {
data: T
}
type ExtractGeneric<T> = T extends Wrapped<infer X> ? X : never
function f<T extends Wrapped<any>>(): ExtractGeneric<T> {
....
}
const n = f<Wrapped<number>>()
这篇关于Typescript推断泛型中的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文