RxDB RxCollectionCreator Typescript错误

时间:2018-05-22 17:37:30

标签: 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添加到我的架构中,它只会抱怨其他遗失的财产,以及其他财产。显然,我做错了什么。任何关于这个问题的智慧都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

可以找到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;

}

架构接口是RxJsonSchema或RxSchema。 另请参阅源代码中的示例。 你的对象似乎是mach RxJsonSchema类型的定义,但是接口中的某些文字缺失或者有一些额外的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
};

}