如何解决如何在TypeScript中为泛型函数创建泛型类型?
我有一个通用函数F:
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,172.26.2.229
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
此函数可分配给泛型类型function f<T>(a: T): T {
return a;
}
// typeof f: <T>(a: T) => T
// let's give it an alias
type FunctionWithGenericParameter = <T>(a: T) => T;
,并带有一个具体的类型参数:
F
但是,类型type F<T> = (a: T) => T;
const f1: F<string> = f;
本身不是通用的。
FunctionWithGenericParameter
我的问题是:我可以通过某种方式将函数f type FunctionWithStringParameter = FunctionWithGenericParameter<string>;
// TS2315: Type 'FunctionWithGenericParameter' is not generic.
的类型转换为类型<T>(a: T) => T
,从而生成类型(a: T) => T
吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。