diff options
Diffstat (limited to 'src/new_fields')
| -rw-r--r-- | src/new_fields/Doc.ts | 8 | ||||
| -rw-r--r-- | src/new_fields/Schema.ts | 7 | ||||
| -rw-r--r-- | src/new_fields/Types.ts | 2 |
3 files changed, 13 insertions, 4 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index c67170573..23a8c05cc 100644 --- a/src/new_fields/Doc.ts +++ b/src/new_fields/Doc.ts @@ -84,6 +84,14 @@ export namespace Doc { export function GetT<T extends Field>(doc: Doc, key: string, ctor: FieldCtor<T>, ignoreProto: boolean = false): Field | null | undefined { return Cast(Get(doc, key, ignoreProto), ctor); } + export function MakeDelegate(doc: Opt<Doc>): Opt<Doc> { + if (!doc) { + return undefined; + } + const delegate = new Doc(); + delegate.prototype = doc; + return delegate; + } export const Prototype = Symbol("Prototype"); } diff --git a/src/new_fields/Schema.ts b/src/new_fields/Schema.ts index c7d2f0801..27b9635af 100644 --- a/src/new_fields/Schema.ts +++ b/src/new_fields/Schema.ts @@ -1,4 +1,4 @@ -import { Interface, ToInterface, Cast } from "./Types"; +import { Interface, ToInterface, Cast, FieldCtor, ToConstructor } from "./Types"; import { Doc } from "./Doc"; export type makeInterface<T extends Interface> = Partial<ToInterface<T>> & Doc; @@ -42,6 +42,7 @@ export function makeStrictInterface<T extends Interface>(schema: T): (doc: Doc) }; } -export function createSchema<T extends Interface>(schema: T): T { - return schema; +export function createSchema<T extends Interface>(schema: T): T & { prototype: ToConstructor<Doc> } { + schema.prototype = Doc; + return schema as any; } diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts index 416298a64..cafb208ce 100644 --- a/src/new_fields/Types.ts +++ b/src/new_fields/Types.ts @@ -53,6 +53,6 @@ export function Cast<T extends FieldCtor<Field>>(field: Field | null | undefined return undefined; } -export function FieldValue<T extends Field>(field: Opt<Field> | Promise<Opt<Field>>): Opt<Field> { +export function FieldValue<T extends Field>(field: Opt<T> | Promise<Opt<T>>): Opt<T> { return field instanceof Promise ? undefined : field; } |
