TypeScript:如何在不指定模板参数的情况下使用模板化类型?

TypeScript:如何在不指定模板参数的情况下使用模板化类型?,typescript,templates,generics,types,typescript-generics,Typescript,Templates,Generics,Types,Typescript Generics,最简单的工作示例: type FuncType<T> = (a: T) => T; let f1: FuncType<T>; // does not compile let f2: <T>(a: T) => T; // compiles type FuncType=(a:T)=>T; 设f1:FuncType;//不编译 设f2:(a:T)=>T;//汇编 问题是FuncType不是我定义的,而是来自一个库。它也比上面的例子复杂得多。我希望使

最简单的工作示例:

type FuncType<T> = (a: T) => T;

let f1: FuncType<T>; // does not compile
let f2: <T>(a: T) => T; // compiles
type FuncType=(a:T)=>T;
设f1:FuncType;//不编译
设f2:(a:T)=>T;//汇编
问题是
FuncType
不是我定义的,而是来自一个库。它也比上面的例子复杂得多。我希望使用从库中导出的模板类型,而不必自己重新声明它(就像我在注释
f2
时所做的那样)

有可能吗?如果没有,你知道为什么没有


感谢您的输入,我找不到有关此主题的任何内容

您不能将变量键入未实例化的泛型类型

之所以
(a:T)=>T
有效,是因为这是一个通用函数
FuncType
,定义为恰好是函数的泛型类型。区别在于,对于泛型函数,类型参数是在调用站点(对于每个调用)确定的,而对于泛型类型,类型是在声明变量时确定的,并且不受后续调用的影响

type FuncType<T> = (a: T) => T;

declare let f1: FuncType<number>;
f1(1) // ok, T is number
f1("1") // not ok, T is still number
declare let f2: <T>(a: T) => T; 
f2(1) // ok, T is number 
f2("1") // also ok, T is now string
type FuncType=(a:T)=>T;
声明let f1:FuncType;
f1(1)//好的,T是数字
f1(“1”)//不正常,T仍然是数字
声明let f2:(a:T)=>T;
f2(1)//好的,T是数字
f2(“1”)//也可以,T现在是字符串


如果要表示
FuncType
的任何实例化,最好选择
FuncType
。但你将不得不忍受相关的不安全因素。(
unknow
never
在这里不起作用,因为
FuncType
t
中是不变的,它们分别适用于
type FuncType=()=>t
type FuncType=(a:t)=>void

声明让f1:FuncType4.2.3
编译的代码>-您使用的是哪种编译器版本@RobertoZvjerković?