如何在不使用任何内容的情况下为PouchDB文档内容设置适当的类型

时间:2018-10-15 17:10:33

标签: typescript pouchdb

spatial_dropout1d

如何为包含PouchDB文档的变量指定正确的类型?

如果我从键入中使用默认的类型继承,则总会得到import PouchDB from 'pouchdb'; const db: PouchDB.Database<Config> = new PouchDB(process.env.POUCH_REMOTE); const orgs = [{login: 'username'}]; interface Config { members: string[]; config: { irc: { server: string port: number nickname: string ident: string realname: string NickServPass: string channel: string sasl: { cert: Buffer | string key: Buffer | string key_passphrase: string } bindhost: string | null requireAuth: boolean notice: boolean } attentionString: string multipleCommitsMaxLen: number detailedDeletesAndCreates: boolean }; repos: { [key: string]: { enabled: boolean } }; } let orgDB: any; //<-- Need to use type `any` here, so we don't get those darn messages for await (const org of orgs) { try { orgDB = await db.get(org.login); } catch (e) { if (e.name === 'not_found') { db.put({ _id: org.login }); orgDB = await db.get(org.login); } } const members: Array<{login: string}> = [{login:"user1"}, {login:"user2"}] for (let { login: member } of members) { if (!orgDB.members!.includes(member)) { //<-- Here the used before assignment happens orgDB.members!.push(member); //<-- Here the used before assignment happens } } const repos: Array<{full_name:string}> = [{full_name:"repo1"},{full_name:"repo-new"}] for (let repo of repos) { if (!orgDB.repos[repo.full_name!]) //<-- Here the used before assignment happens orgDB.repos[repo.full_name!] = { enabled: true }; //<-- Here the used before assignment happens } db.put(orgDB); //<-- Here the used before assignment happens } 。 现在,当我使用创建的类型和Property 'x' does not exist on type 'IdMeta & GetMeta'.接口并尝试将新文档推送到db时,我得到一条相反的消息,即该属性丢失。

我希望使用正确的类型而不是PouchDB.Database,因为它不是最好的。有任何想法或解决方法吗?

0 个答案:

没有答案