typescript - RxDB RxCollectionCreator typescript 错误

标签 typescript rxjs electron

这是我第一次使用 RxDB,我遇到了一个奇怪的 Typescript 错误。

我的 Electron 项目的相关部分如下:

import RxDB, { RxCollection, RxDatabase } from "rxdb";
RxDB.plugin(require("pouchdb-adapter-idb"));

// Took this from the Electron example on Github so I knew I had a valid JSONSchema
const heroSchema = {
  title: 'hero schema',
  description: 'describes a simple hero',
  version: 0,
  type: 'object',
  properties: {
    name: {
      type: 'string',
      primary: true
    },
    color: {
      type: 'string'
    }
  },
  required: ['color']
};

// These lines are actually in an async function so the await keyword is not an issue
const db = await RxDB.create({ name: "heroedb", adapter: "idb" });

const devices = await db.collection({
     name: "herocollection",
     schema: heroSchema
});

但 typescript 提示:
Argument of type '{ name: string; schema: { title: string; description: string; version: number; type: string; prop...' is not assignable to parameter of type 'RxCollectionCreator'.
Types of property 'schema' are incompatible.
Type '{ title: string; description: string; version: number; type: string; properties: { name: { type: ...' is not assignable to type 'RxJsonSchema | RxSchema<any>'.
Type '{ title: string; description: string; version: number; type: string; properties: { name: { type: ...' is not assignable to type 'RxSchema<any>'.
Property 'jsonID' is missing in type '{ title: string; description: string; version: number; type: string; properties: { name: { type: ...'

如果我添加 jsonID对于我的架构,它只是提示其他丢失的属性,以及其他和其他。显然,我做错了什么。任何关于这个问题的智慧将不胜感激。

最佳答案

RxDB 源代码可以找到 here .
当您使用 RxSchema.create({...}) 函数时,它实际上创建了类 RxSchema 的实例,因此类型肯定会匹配。
否则,集合创建者的界面如下:

export interface RxCollectionCreator {
name: string;
schema: RxJsonSchema | RxSchema;
pouchSettings?: PouchSettings;
migrationStrategies?: {
    [key: number]: Function
};
autoMigrate?: boolean;
statics?: {
    [key: string]: Function
};
methods?: {
    [key: string]: Function
};
attachments?: {
    [key: string]: Function
};
options?: any;

}

模式接口(interface)是 RxJsonSchema 或 RxSchema。
另请参阅源代码中的示例。
您的对象似乎与 RxJsonSchema 类型定义相匹配,但是接口(interface)中的某些文字丢失了,或者 TypeScript 提示了一些额外的内容。
export declare class RxJsonSchema {
title?: string;
description?: string;
version: number;
type: 'object';
properties: { [key: string]: RxJsonSchemaTopLevel };
required?: Array<string>;
compoundIndexes?: Array<string | Array<string>>;
disableKeyCompression?: boolean;
additionalProperties?: true;
attachments?: {
        encrypted?: boolean
};

}

关于typescript - RxDB RxCollectionCreator typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50473560/

相关文章:

javascript - angular rxjs 管道从数组中删除元素

javascript - 仅前端的 Electron

javascript - 如何在 javascript 中使用 RoutePrefix 获取 Web API 方法的有效 url

javascript - rxjs 中 do 和 subscribe 的区别

javascript - 如何在TypeScript定义中定义ReactFunctionalComponent的返回类型

javascript - RxJs 将流拆分为多个流

electron - 我们可以创建一个连接到在线版本的 HyperDev Electron 应用程序吗?

javascript - 如何使用新的 html 文件打开更新 Electron 浏览器窗口

typescript - Cypress + Typescript : How to use JS methods on return values of type Cypress. Chainable<字符串>?

typescript - 对数组使用字符串索引可以避免类型安全?