[英]Specialize a type for generic function
Given this definition:鉴于此定义:
declare function foo<T>(): { bar: T }
// <T>() => { bar: T }
type Foo = typeof foo;
How can one provide specialization to the generic function by its type?如何通过类型为泛型函数提供特化?
What I want to achieve is to be able to do something like this:我想要实现的是能够做这样的事情:
// { bar: number }
type FooResult = ReturnType<Foo<number>>;
But TypeScript complaints that Foo
itself is not generic - the function it types is.但是 TypeScript 抱怨
Foo
本身不是通用的——它键入的函数是通用的。
TypeScript doesn't really support the kind of higher-order typing you'd need to get this from ReturnType
... it's a known design limitation . TypeScript 并不真正支持从
ReturnType
获取它所需的那种高阶类型......这是一个已知的设计限制。 So all you have available is a variety of workarounds.因此,您所拥有的只是各种解决方法。 Here are the ones I can think of off the top of my head:
以下是我能想到的那些:
Do it manually.手动执行。 This is essentially a non-answer, but could be the best way forward since it doesn't rely on any weird type system tricks:
这本质上是一个非答案,但可能是最好的前进方式,因为它不依赖于任何奇怪的类型系统技巧:
type FooResult<T> = { bar: T }; type FooResultNumber = FooResult<number>; // {bar: number}
Pretend to actually call foo()
and get its result.假装实际调用
foo()
并得到它的结果。 TypeScript doesn't support arbitrary type queries , so type FooResult = typeof foo<number>()
unfortunately does not compile. TypeScript 不支持任意类型查询,因此
type FooResult = typeof foo<number>()
不幸的是不能编译。 The following code is about as close as you can get:以下代码尽可能接近:
const __dummy = (true as false) || foo<number>(); type FooResultNumber = typeof __dummy; // {bar: number}
This introduces a dummy variable in your runtime code.这会在您的运行时代码中引入一个虚拟变量。 The
(true as false) || expression
(true as false) || expression
(true as false) || expression
construct uses a type assertion to lie to the compiler. (true as false) || expression
构造使用类型断言来欺骗编译器。 The compiler thinks you are doing false || expression
编译器认为你在做
false || expression
false || expression
, the type of which will be the same as the type of expression
. false || expression
,其类型将与expression
的类型相同。 What you are really doing at runtime is true || expression
你在运行时真正在做的事情是
true || expression
true || expression
which short-circuits, returning true
without ever evaluating expression
.短路的
true || expression
,返回true
而不评估expression
。 This means that foo()
never gets called at runtime despite being in the code.这意味着
foo()
尽管在代码中,但永远不会在运行时被调用。
Another way to pretend to call foo()
is with a dummy class... you will never instantiate the class but it lets the compiler reason about types:另一种假装调用
foo()
是使用一个虚拟类......你永远不会实例化这个类,但它让编译器推断类型:
class __FooRunner<T> { result = foo<T>(); } type FooResult<T> = __FooRunner<T>["result"]; type FooResultNumber = FooResult<number>; // {bar: number}
Again, this puts some junk in your runtime code, which may or may not be acceptable depending on your use cases.同样,这会在您的运行时代码中放置一些垃圾,根据您的用例,这可能会或可能不会被接受。
Okay, hope that helps;好的,希望有帮助; good luck!
祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.