我已经在打字稿中创建了一个接口,并试图在函数中使用它,但出现错误。
任何想法,为什么我会看到此错误。谢谢...
index.d.ts:
export interface Test {
TS: moment.Moment | Date | string;
ID: string;
QTY: number;
PROD_DATE?: moment.Moment | Date | string | null;
PROD_DATE_TO?: moment.Moment | Date | string | null;
STATUS: number;
ALLOC_QTY?: number | null | undefined;
}
index.ts
const buildQuery = (row: Test): [string, Test[]] => {
let query = `INSERT INTO TEST(TS, ID, QTY, PROD_DATE, PROD_DATE_TO, STATUS, ALLOC_QTY, INS_DATE)
VALUES (CAST($1 AS TIMESTAMP WITH TIME ZONE), $2, $3,
CAST($4 AS TIMESTAMP WITH TIME ZONE),
CAST($5 AS TIMESTAMP WITH TIME ZONE), $6, $7, now()); `;
let insertUpdateValues = [
moment(row.TS, "D-MMM-YY hh.mm.ss.SSSSSS A Z"), row.ID, row.QTY,
row.PROD_DATE ? moment(row.PROD_DATE, "D-MMM-YY hh.mm.ss.SSSSSS A Z"): null,
row.PROD_DATE_TO ? moment(row.PROD_DATE_TO , "D-MMM-YY hh.mm.ss.SSSSSS A Z"): null,
row.STATUS, row.ALLOC_QTY
];
return [insertQuery, insertUpdateValues];
}
const runQuery = () =>{
const queryData = buildQuery();
client.query(queryData[0], queryData[1]);
}
{p}的签名是client.query
错误
query(queryTextOrConfig: string | QueryConfig, values?: any[]): Promise<QueryResult>;
答案 0 :(得分:0)
您想要的是为每个条目定义一个具有固定类型的数组,这称为元组。您可以将其定义为元组,如下所示:
type Test = [
moment.Moment | Date | string,
string,
number,
moment.Moment | Date | string | null,
moment.Moment | Date | string | null,
number,
number | null | undefined,
]
const buildQuery = (row: Test): [string, Test] => {
// ...
}
这样,您可以从函数中返回一个值数组。然后TypeScript将检查每个值是否与该索引处的预期类型匹配。