如何解决 PouchDB 的错误 (?) TypeScript/DefinitelyTyped 类型?
How to work around incorrect (?) TypeScript/DefinitelyTyped typings for PouchDB?
我正在寻求有关 TypeScript 和 PouchDB type declarations 的帮助。考虑这个 TS 代码:
import PouchDB = require('pouchdb')
import find = require('pouchdb-find')
PouchDB.plugin(find)
const data = new PouchDB("http://localhost:5984/data"),
export async function deleteLastRevForIds(dbname, ids) {
const docsToDelete = await data.find({
fields: ["_id", "_rev"],
selector: { _id: { $in: ids } }
})
const deletePromise = docsToDelete.docs.map(doc => {
return data.remove(doc) // <-- HERE TSC SHOUTS AT ME about `doc`
})
const deletion = await Promise.all(deletePromise)
return deletion
}
在带注释的 remove()
调用中,tsc
发出此错误:
Argument of type 'IdMeta' is not assignable to parameter of type 'RemoveDocument'.
Type 'IdMeta' is not assignable to type 'RevisionIdMeta'.
Property '_rev' is missing in type 'IdMeta'.'
发生的事情是 find()
调用是 typed by the DefinitelyTyped typings
作为 returning {docs: PouchDB.Core.IdMeta[]}
。正如它的名字所暗示的那样,
PouchDB.Core.IdMeta[] 表示 {_id: string}
.
的数组
但这是假的! PouchDB.find()
不 return 只是 {_id: ...}
个对象的列表,
它 return 是 {_id: ..., _rev: ...}
的列表(另外我明确要求这两个字段)。还是我遗漏了什么?
因此,当调用 remove()
函数时(按 DT 类型正确输入为
需要一个完全指定的 _id+_rev RevisionIdMeta
) 和这样的对象,TS 正确地冲我大喊。
我试着向各个方向投掷这个东西,但无法按照我的意愿弯曲它; tsc
一直报错说我的对象中缺少 _rev
。
- 我有办法做这样的 "deep" 转换吗?
- 如果不是,有没有办法尽可能在本地覆盖 DT 类型?
- 我应该完全做些别的事情吗?
此外,我是否应该建议更改 DT 类型?
谢谢。
Is there a way for me do such a "deep" cast?
您可以随时使用 as any as Whatever
重新施放一些东西。显然,如果您无法获得正确的类型,这应该是最后的手段,因为断言周围没有任何安全性。例如 123 as any as Window
编译。
is there a way to override the DT typings as locally as possible?
是的,看看Declaration Merging: Module Augmentation。
Should I be doing something else entirely?
如果您确定类型定义有误,您可以随时修补它们并向 DefinitelyTyped 提交 PR。
是的,Pouchdb-find 定义是错误的。这样的事情应该会让你感动...
const data = new PouchDB("http://localhost:5984/data")
interface FixedFindResponse<Content extends PouchDB.Core.Encodable> {
docs: PouchDB.Core.ExistingDocument<Content>[];
}
export async function deleteLastRevForIds(dbname: void, ids: number[]) {
const docsToDelete: FixedFindResponse<any> = await data.find({
fields: ["_id", "_rev"],
selector: { _id: { $in: ids } }
})
const deletePromise = docsToDelete.docs.map(doc => {
return data.remove(doc) // <-- HERE TSC SHOUTS AT ME about `doc`
})
const deletion = await Promise.all(deletePromise)
return deletion
}
我正在寻求有关 TypeScript 和 PouchDB type declarations 的帮助。考虑这个 TS 代码:
import PouchDB = require('pouchdb')
import find = require('pouchdb-find')
PouchDB.plugin(find)
const data = new PouchDB("http://localhost:5984/data"),
export async function deleteLastRevForIds(dbname, ids) {
const docsToDelete = await data.find({
fields: ["_id", "_rev"],
selector: { _id: { $in: ids } }
})
const deletePromise = docsToDelete.docs.map(doc => {
return data.remove(doc) // <-- HERE TSC SHOUTS AT ME about `doc`
})
const deletion = await Promise.all(deletePromise)
return deletion
}
在带注释的 remove()
调用中,tsc
发出此错误:
Argument of type 'IdMeta' is not assignable to parameter of type 'RemoveDocument'.
Type 'IdMeta' is not assignable to type 'RevisionIdMeta'.
Property '_rev' is missing in type 'IdMeta'.'
发生的事情是 find()
调用是 typed by the DefinitelyTyped typings
作为 returning {docs: PouchDB.Core.IdMeta[]}
。正如它的名字所暗示的那样,
PouchDB.Core.IdMeta[] 表示 {_id: string}
.
但这是假的! PouchDB.find()
不 return 只是 {_id: ...}
个对象的列表,
它 return 是 {_id: ..., _rev: ...}
的列表(另外我明确要求这两个字段)。还是我遗漏了什么?
因此,当调用 remove()
函数时(按 DT 类型正确输入为
需要一个完全指定的 _id+_rev RevisionIdMeta
) 和这样的对象,TS 正确地冲我大喊。
我试着向各个方向投掷这个东西,但无法按照我的意愿弯曲它; tsc
一直报错说我的对象中缺少 _rev
。
- 我有办法做这样的 "deep" 转换吗?
- 如果不是,有没有办法尽可能在本地覆盖 DT 类型?
- 我应该完全做些别的事情吗?
此外,我是否应该建议更改 DT 类型?
谢谢。
Is there a way for me do such a "deep" cast?
您可以随时使用 as any as Whatever
重新施放一些东西。显然,如果您无法获得正确的类型,这应该是最后的手段,因为断言周围没有任何安全性。例如 123 as any as Window
编译。
is there a way to override the DT typings as locally as possible?
是的,看看Declaration Merging: Module Augmentation。
Should I be doing something else entirely?
如果您确定类型定义有误,您可以随时修补它们并向 DefinitelyTyped 提交 PR。
是的,Pouchdb-find 定义是错误的。这样的事情应该会让你感动...
const data = new PouchDB("http://localhost:5984/data")
interface FixedFindResponse<Content extends PouchDB.Core.Encodable> {
docs: PouchDB.Core.ExistingDocument<Content>[];
}
export async function deleteLastRevForIds(dbname: void, ids: number[]) {
const docsToDelete: FixedFindResponse<any> = await data.find({
fields: ["_id", "_rev"],
selector: { _id: { $in: ids } }
})
const deletePromise = docsToDelete.docs.map(doc => {
return data.remove(doc) // <-- HERE TSC SHOUTS AT ME about `doc`
})
const deletion = await Promise.all(deletePromise)
return deletion
}