打字稿通用类型函数以获取给定类型的名称
我正在使用GraphQl-Codegen为我的GraphQl Server生成类型,还使用相同的模式为我的DB生成类型(不完全相同),使用Mongo插件用于Codegen,
我配置了Codegen,我将Codegen与名称< typeName>
适用于DB模式的合适类型,该类型< typeName> db
,因此对于类型Person
输入数据库将为persondb
。
我需要映射这些更改,以使CodeGen生成安全的GraphQl解析器,我可以使用静态词典进行操作,但是我更喜欢通过映射名称自动进行。
因此,是否有可能使用TypeScript,获取通用类型t
传递给通用TypeScript函数的类型名称的名称?
这是实现的示例,唯一缺少的部分是getName
generic。
// generated.ts
// automatically generated
type Person = {
name: string;
};
type PersonDB = {
name: string;
_id: string;
};
// mapper.ts
import * as Types from "./generated";
// Types["Person"] => Person Type
// type mapper<T> = ...
// type m = mapper<Person>
// type of m should be PersonDB
//possible implementation
type getName<T> = any // todo:how?
type mapper<T> = Types[`${getName<T>}DB`];
替代自动化过程也将不胜感激。
相关但不相同:
I'm using graphql-codegen to generate types for my graphql server and also using the same schema to generate types for my DB(which is not exactly the same) using mongo plugin for codegen
I've configured codegen to generate to each type with the name <typeName>
an appropriate type for the DB schema with the name <typeName>DB
, so for the type Person
the appropriate type in the DB will be PersonDB
.
I need to map these changes in order for codegen to generate a safe graphql resolvers, I can do it with a static dictionary but I prefer doing it automatically by mapping the names.
so, is it possible, using typescript, getting the name of a type name for a generic type T
passed to generic typescript function?
here's an example for implementation, when the only missing piece is the getName
generic.
// generated.ts
// automatically generated
type Person = {
name: string;
};
type PersonDB = {
name: string;
_id: string;
};
// mapper.ts
import * as Types from "./generated";
// Types["Person"] => Person Type
// type mapper<T> = ...
// type m = mapper<Person>
// type of m should be PersonDB
//possible implementation
type getName<T> = any // todo:how?
type mapper<T> = Types[`${getName<T>}DB`];
an alternative automated process would also be very appreciated.
relevant but not the same:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论