我正在输入一种查询引擎.以下是到目前为止的情况:
type WhereClause = { [key: string]: string | number | boolean };
type $Option = { "$"?: { where: WhereClause } };
type Subquery = { [namespace: string]: NamespaceVal };
type NamespaceVal = $Option & Subquery | $Option;
type Query = { [namespace: string]: NamespaceVal };
type InstantObject = {
id: string;
[prop: string]: any;
};
type Responsify<T> = {
[P in keyof T]: T[P] extends object
? (Responsify<Omit<T[P], "$">> & { id: string; [k: string]: any })[]
: T[P][];
};
function queryResponse<T extends Query>(q: T): Responsify<T> {
return 1 as any
}
const resOne = queryResponse({users: {$: {where: {"foo": 1}}, comments: {}}});
这是可行的,但输出类型非常冗长:
declare const resOne: Responsify<{
users: {
$: {
where: {
foo: number;
};
};
comments: {};
};
}>;
这可能会使最终用户很难通过IntelliSense阅读.
问题:
有没有办法,我可以产生一个"更干净"的输出类型?
一个明显的胜利,如果我可以完全省略$:
declare const resOne: Responsify<{
users: {
comments: {};
};
}>;