我有以下打字代码片段:
type Query = {
model: string;
on: string;
fields: string[];
query?: Query[];
};
function getModel(config: {
object: xmlrpc.Client
db: string
userId: number
password: string
}) {
return <T extends { [key: string]: any }>(
model: string,
fields: Extract<keyof T, string>[],
props?: {
offset?: number
limit?: number
where?: [string, string, any][]
query?: Query[]
}
): Promise<(T & { id: number })[]> =>
new Promise((resolve, reject) => {
// Implementation details
}).then(async (res) => {
// Post-processing logic
});
}
当调用getModel
时,result
的推断类型不符合我的期望.下面是我如何调用该函数:
const config = { object, db, userId, password };
const result = await getModel(config)(
"product.template",
["name", "categ_id", "attribute_line_ids", "service_type"],
{
limit: 5,
where: [["categ_id", "=", 40]],
query: [
{
model: "product.template.attribute.line",
on: "attribute_line_ids",
fields: ["value_ids"],
query: [
{
model: "product.attribute.value",
on: "value_ids",
fields: ["name"],
},
],
},
],
}
);
我预计result
的类型是:
const result: ({
name: any
categ_id: any
attribute_line_ids: ({ value_ids: ({ name: any } & { id: number })[] } & {
id: number
})[]
service_type: any
} & { id: number })[]
如何调整泛型类型定义以确保推断的类型与预期 struct 匹配?