Javascript 在typescript中具有动态返回的函数

Javascript 在typescript中具有动态返回的函数,javascript,node.js,typescript,Javascript,Node.js,Typescript,我正在尝试用typescript在NodeJS上创建一个API 我有以下接口: export interface ISqlResonse<T> { success?: string error?: string data: Array<T> //Nothing | array of object when are db operations } export interface IApiResponse<T> { status:

我正在尝试用typescript在NodeJS上创建一个API

我有以下接口:

export interface ISqlResonse<T> {
success?:   string
error?:     string
data:       Array<T> //Nothing | array of object when are db operations
}

export interface IApiResponse<T> {
status:     'error' | 'success'
message:    string
data:       Array<T>
}
导出接口ISqlResonse{
成功?:字符串
错误?:字符串
data:Array//Nothing |对象的数组当是db操作时
}
导出接口IApiResponse{
状态:“错误”|“成功”
消息:string
数据:数组
}
每个api调用调用一个函数,该函数调用从数据库中选择/插入/更新/删除数据的通用类名DB 例如,更新函数如下所示:

 async updateData(input: IUpdateParam) : Promise<ISqlResonse<object>> {
    ...
    ...
}
async update(req): Promise<IApiResponse<IAccessPointsTableStructure>> {
    let data        = req.body ;
    let updateObj   = {
        data ,
        table: 'accessPoints',
        excludeColumns: 'loggedUserId',
        additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
        validationRules,
        where: `id=${data.id}`,
        returningData: true
    }

    let sqlResults = await db.updateData(updateObj) ; //  !!!

    if(typeof sqlResults.error==="string") {
        logger.log('error','Error on updating Access Points!',{sql: db.getQuery(), error: sqlResults.error});
        return({status:'error',message: 'Error on updating Access Points!',data: sqlResults.data});
    }

    logger.log('success', 'Access Points data updated with success!');
    return({status: 'error', message: 'Access Points data updated with success!', data: sqlResults.data})
}
异步更新数据(输入:IUpdateParam):承诺{ ... ... } API函数调用DB,如下所示:

 async updateData(input: IUpdateParam) : Promise<ISqlResonse<object>> {
    ...
    ...
}
async update(req): Promise<IApiResponse<IAccessPointsTableStructure>> {
    let data        = req.body ;
    let updateObj   = {
        data ,
        table: 'accessPoints',
        excludeColumns: 'loggedUserId',
        additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
        validationRules,
        where: `id=${data.id}`,
        returningData: true
    }

    let sqlResults = await db.updateData(updateObj) ; //  !!!

    if(typeof sqlResults.error==="string") {
        logger.log('error','Error on updating Access Points!',{sql: db.getQuery(), error: sqlResults.error});
        return({status:'error',message: 'Error on updating Access Points!',data: sqlResults.data});
    }

    logger.log('success', 'Access Points data updated with success!');
    return({status: 'error', message: 'Access Points data updated with success!', data: sqlResults.data})
}
异步更新(req):承诺{ 让data=req.body; 让updateObj={ 数据, 表:“访问点”, excludeColumns:'loggedUserId', 附加列:{modifiedBy:'1',modifiedAt:crtDate}, 验证规则, 其中:`id=${data.id}`, 返回数据:true } 让sqlResults=wait db.updateData(updateObj);/!!! if(sqlResults.error的类型==“string”){ logger.log('error','error on updated Access Points!',{sql:db.getQuery(),error:sqlResults.error}); 返回({状态:'error',消息:'error on updateing Access Points!',数据:sqlResults.data}); } log('success','Access Points data updated with success!'); 返回({状态:'error',消息:'Access Points data updated with success!',数据:sqlResults.data}) } 我的问题是:如何调用函数db.updateData(),并告诉此函数我希望从ISqlResponse接收包含接口IAccessPointsTableStructure等对象的数组中的数据

换句话说,我想控制函数的返回类型。我用不同的方法测试了几次。(替换数据库中的wit.updateData(…)。。。
谢谢您的建议。

您还没有包括
IUpdateParam
的定义,但是我假设它的
属性决定了
updateData()
返回的内容的类型。我所评论的所有“猜测”都只是一个例子;您应该更改它们以适合您的用例


您应该能够修改
updateData()
的签名,以反映传入的
IUpdateParam
类型与返回的
Promise
类型之间的关系。首先,声明一个类型以表示每个表的表名和数据类型之间的映射。例如:

export type TableNameToTypeMapping = {
  accessPoints: IAccessPointsTableStructure,
  otherThings: IOtherThingsTableStructure, // guess
  // ...
}
现在,您可以更改
IUpdateParam
的定义,使其只接受
表的正确值:

export interface IUpdateParam<K extends keyof TableNameToTypeMapping> {
        data: any, // guess
        table: K, 
        excludeColumns: string, // guess
        additionalColumns: any, // guess
        validationRules: any, // guess
        where: string // guess
}
这意味着如果您使用类型为
IUpdateParam
的参数调用
updateData
,您将返回一个
Promise
。但是
tablename-totypemapping['accessPoints']
只是
iaccesspointstableststructure
,因此您将根据需要返回一个
Promise


请注意,对象literal
updateObj
将其
table
属性推断为type
string
,这太宽了。要确保调用
updateData()
根据需要工作,您需要断言
updateObj.table
属性是文本类型
'accessPoints'
,如下所示:

let updateObj = {
    data,
    table: 'accessPoints' as 'accessPoints', // assertion
    excludeColumns: 'loggedUserId',
    additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
    validationRules,
    where: `id=${data.id}`,
    returningData: true
}
// type declaration
let updateObj:IUpdateParam<'accessPoints'> = {
    data ,
    table: 'accessPoints',
    excludeColumns: 'loggedUserId',
    additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
    validationRules,
    where: `id=${data.id}`,
    returningData: true
}
或者您需要声明
updateObj
的类型为
IUpdateParam
,如下所示:

let updateObj = {
    data,
    table: 'accessPoints' as 'accessPoints', // assertion
    excludeColumns: 'loggedUserId',
    additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
    validationRules,
    where: `id=${data.id}`,
    returningData: true
}
// type declaration
let updateObj:IUpdateParam<'accessPoints'> = {
    data ,
    table: 'accessPoints',
    excludeColumns: 'loggedUserId',
    additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
    validationRules,
    where: `id=${data.id}`,
    returningData: true
}
//类型声明
让updateObj:IUpdateParam={
数据,
表:“访问点”,
excludeColumns:'loggedUserId',
附加列:{modifiedBy:'1',modifiedAt:crtDate},
验证规则,
其中:`id=${data.id}`,
返回数据:true
}
任何一种方法都应该有效



希望这会有所帮助;祝你好运!

你还没有包括
IUpdateParam
的定义,但我会假设它的
属性决定了
updateData()
返回的内容的类型。我所评论的所有“猜测”都只是一个例子;你应该更改它们以适合你的用例


您应该能够修改
updateData()
的签名,以反映传入的
IUpdateParam
类型与返回的
Promise
类型之间的关系。首先,声明一个类型以表示每个表的表名和数据类型之间的映射。例如:

export type TableNameToTypeMapping = {
  accessPoints: IAccessPointsTableStructure,
  otherThings: IOtherThingsTableStructure, // guess
  // ...
}
现在,您可以更改
IUpdateParam
的定义,使其只接受
表的正确值:

export interface IUpdateParam<K extends keyof TableNameToTypeMapping> {
        data: any, // guess
        table: K, 
        excludeColumns: string, // guess
        additionalColumns: any, // guess
        validationRules: any, // guess
        where: string // guess
}
这意味着如果您使用类型为
IUpdateParam
的参数调用
updateData
,您将返回一个
Promise
。但是
tablename-totypemapping['accessPoints']
只是
iaccesspointstableststructure
,因此您将根据需要返回一个
Promise


请注意,对象literal
updateObj
将其
table
属性推断为type
string
,这太宽了。要确保调用
updateData()
根据需要工作,您需要断言
updateObj.table
属性是文本类型
'accessPoints'
,如下所示:

let updateObj = {
    data,
    table: 'accessPoints' as 'accessPoints', // assertion
    excludeColumns: 'loggedUserId',
    additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
    validationRules,
    where: `id=${data.id}`,
    returningData: true
}
// type declaration
let updateObj:IUpdateParam<'accessPoints'> = {
    data ,
    table: 'accessPoints',
    excludeColumns: 'loggedUserId',
    additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
    validationRules,
    where: `id=${data.id}`,
    returningData: true
}
或者您需要声明
updateObj
的类型为
IUpdateParam
,如下所示:

let updateObj = {
    data,
    table: 'accessPoints' as 'accessPoints', // assertion
    excludeColumns: 'loggedUserId',
    additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
    validationRules,
    where: `id=${data.id}`,
    returningData: true
}
// type declaration
let updateObj:IUpdateParam<'accessPoints'> = {
    data ,
    table: 'accessPoints',
    excludeColumns: 'loggedUserId',
    additionalColumns: { modifiedBy: '1', modifiedAt: crtDate },
    validationRules,
    where: `id=${data.id}`,
    returningData: true
}
//类型声明
让updateObj:IUpdateParam={
数据,
表:“访问点”,
excludeColumns:'loggedUserId',
附加列:{modifiedBy:'1',modifiedAt:crtDate},
验证规则,
其中:`id=${data.id}`,
返回数据:true
}
任何一种方法都应该有效


希望对您有所帮助;祝您好运!

转换返回值:
(wait db.updateData(updateObj))
?可能是我误解了