Typescript RxDB RxCollectionCreator类型脚本错误

Typescript RxDB RxCollectionCreator类型脚本错误,typescript,rxjs,electron,Typescript,Rxjs,Electron,这是我第一次使用RxDB,我遇到了一个奇怪的类型脚本错误 我的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 heroSchem

这是我第一次使用RxDB,我遇到了一个奇怪的类型脚本错误

我的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: ...'
“{name:string;schema:{title:string;description:string;version:number;type:string;prop…”类型的
参数不能分配给“RxCollectionCreator”类型的参数。
属性“架构”的类型不兼容。
类型“{title:string;description:string;version:number;Type:string;properties:{name:{Type:…”不可分配给类型“RxJsonSchema | RxSchema”。
类型“{title:string;description:string;version:number;Type:string;properties:{name:{Type:…”不可分配给类型“RxSchema”。
类型“{title:string;description:string;version:number;type:string;属性:{name:{type:…”中缺少属性“jsonID”

如果我将
jsonID
添加到我的模式中,它只会抱怨其他缺少的属性,以及其他。显然,我做了一些错误的事情。任何关于这个问题的智慧都将不胜感激。

可以找到RxDB源代码。 当您使用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。 另请参见源代码中的示例。 您的对象似乎有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
};
导出声明类RxJsonSchema{
标题?:字符串;
description?:字符串;
版本:编号;
类型:“对象”;
属性:{[key:string]:RxJsonSchemaTopLevel};
必需?:数组;
复合索引?:数组;
disableKeyCompression?:布尔值;
附加属性:true;
附件?:{
加密?:布尔值
};

}

请参见类型定义,scema可能属于RxJsonSchema或RxSchema类型,不确定缺少什么或什么extra@DennisR有趣。如果我设置heroSchema=RxSchema.create({…})错误消失了。我想知道为什么另一种方法不起作用,因为它在文档中是这样显示的。我理解你的意思,但我不知道遗漏了什么/extra,Typescript错误似乎完全没有用。我发布的代码正是发出错误的代码(除了
await
调用被包装在一个异步函数中)。你能看到我遗漏了什么吗?我已经查看了很多次类型定义,但我想我还是坚持使用
RxSchema.create({…})
吧。我已经和创建者联系过了,他也被难倒了。