有一个 getList 的函数,总共有两种type。每种 type 返回不同的list。
图上我知道写的不对,但是不知道咋写。求指教
(PS:用的web sql,老代码改造ts)
简单奉上代码:
async getDBDate() {
let tabList = (await this.getList('TABS'));
let infoList = (await this.getList('INFOS'));
if (tabList.length === 0) {
// 一些其他操作
}
}
async getList<K,V = 'TABS' | 'INFOS'>(type: K extends keyof V) {
return (await this.executeSql(`SELECT * FROM ${type}`)) as K extends 'TABS' ? T.TabList[] : T.InfoList[];
}
用函数重载,
刚好看到有人提了类似问题:
https://segmentfault.com/q/10...