【发布时间】:2017-10-23 16:41:23
【问题描述】:
我正在寻求有关 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
作为返回{docs: PouchDB.Core.IdMeta[]}。正如它的名字所暗示的那样,
PouchDB.Core.IdMeta[] 表示 {_id: string} 的数组。
但这是错误的! PouchDB.find() 不只返回{_id: ...} 对象的列表,
它返回{_id: ..., _rev: ...} 的列表(另外我明确要求这两个字段)。还是我错过了什么?
因此,当调用remove() 函数时(由 DT 类型正确键入为
需要一个完全指定的 _id+_rev RevisionIdMeta) 和这样的对象,TS 对我大喊大叫。
我试着把这个东西扔到各个方向,但无法按照我的意愿弯曲它; tsc 不断出错,我的对象中缺少 _rev。
- 有没有办法让我做这样一个“深度”演员?
- 如果没有,有没有办法尽可能在本地覆盖 DT 类型?
- 我应该完全做其他事情吗?
另外,我是否应该提议更改 DT 分型?
谢谢。
【问题讨论】:
标签: typescript pouchdb typescript-typings definitelytyped