Knex + Typescript - 部分不可分配给类型字符串
Knex + Typescript - Partial not assignable to type string
我有一个继承的代码库,里面到处都是这样的查询辅助方法:
export async function deleteById(id: string): Promise<string> {
const [deletedId] = await db().queryBuilder()
.delete()
.from('inventory_alert')
.where({id})
.returning('id');
return deletedId;
}
编译打字稿时出现此错误:
Type 'Partial<{}>' is not assignable to type 'string'.
该项目正在使用 Knex,函数中的链接来自该库。我是 Typescript 和 Knex 的新手,所以我很有可能遗漏了一些基本知识。我该如何解决这些错误?
据我所知,from
方法的类型采用 2 个参数,其中第一个是返回记录。
如果您设置该参数,tsc
可能会推断出正确的事情。这是示例:
interface TRecord {
id: string
// ...
}
export async function deleteById(id: string): Promise<string> {
const [deletedId] = await db().queryBuilder()
.delete()
.from<TRecord>('inventory_alert') // set defined record type
.where({id})
.returning('id');
return deletedId;
}
我有一个继承的代码库,里面到处都是这样的查询辅助方法:
export async function deleteById(id: string): Promise<string> {
const [deletedId] = await db().queryBuilder()
.delete()
.from('inventory_alert')
.where({id})
.returning('id');
return deletedId;
}
编译打字稿时出现此错误:
Type 'Partial<{}>' is not assignable to type 'string'.
该项目正在使用 Knex,函数中的链接来自该库。我是 Typescript 和 Knex 的新手,所以我很有可能遗漏了一些基本知识。我该如何解决这些错误?
据我所知,from
方法的类型采用 2 个参数,其中第一个是返回记录。
如果您设置该参数,tsc
可能会推断出正确的事情。这是示例:
interface TRecord {
id: string
// ...
}
export async function deleteById(id: string): Promise<string> {
const [deletedId] = await db().queryBuilder()
.delete()
.from<TRecord>('inventory_alert') // set defined record type
.where({id})
.returning('id');
return deletedId;
}