当我需要Typescript类型时,RealmJS对象返回RealmObject

时间:2019-05-30 17:11:29

标签: typescript realm

我将RealmJS与TypeScript一起使用,并且我已经创建了一个架构和所有必要的步骤,它可以保存,并且当我检索它时,它的类型为RealmObject,即使我明确指定了它的类型。我将此传递给另一个期望为Party数组的函数,但由于不是那样而感到困惑。通过在Party中创建一个使用RealmObject并使用Object.assign()的构造函数,我找到了解决方法。我的问题是,我做错了什么,从Realm返回的对象永远不会得到指定的类型,或者这是您当前必须使用的方式吗?

import { NameBadge } from './NameBadge'
import { WalkInConfig } from './WalkInConfig'
import { Grouping } from './Grouping'
import { Survey } from './Survey'
import { NamecardQuota } from './NameCardQuota'
import { Question } from './Question'
import { Expose } from 'class-transformer'

import Realm from 'realm'

export class Party {
  id!: number
  name!: string
  ...// ommitted for brevity
  static schema: Realm.ObjectSchema

  constructor(realmObject: Realm.Object) {
    Object.assign(this, realmObject)
  }
}

Party.schema = {
    name: 'Party',
    primaryKey: 'id',
    properties: {
    id: 'int',
    name: 'string',
    startAt: 'date',
    endAt: 'date',
    place: 'string?',
    details: 'string?',
      logo: 'string',
      logoBnw: 'string',
      attendeeIds: 'int[]',
      groupingIds: 'int[]',
      updatedAt: 'date',
      createdAt: 'date',
      customAttendeeFieldNames: 'string[]',
      updatedAttendeeIds: 'int[]',
      attendeeCustomLine1: 'string?',
      attendeeCustomLine2: 'string?'
    }
}

我如何找回它

let existingParties = Array.from(realm.objects<Party>(Party.schema.name))

0 个答案:

没有答案