From fca16fb81f470b43c72c1e24e530fdf66430dc7a Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Fri, 26 Apr 2019 21:41:16 -0400 Subject: KVP stuff --- src/new_fields/Doc.ts | 8 ++++++++ src/new_fields/Types.ts | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src/new_fields') diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index 6dfa37c1b..a1515fe49 100644 --- a/src/new_fields/Doc.ts +++ b/src/new_fields/Doc.ts @@ -34,6 +34,13 @@ export class ObjectField { readonly [Id] = ""; } +export function IsField(field: any): field is Field { + return (typeof field === "string") + || (typeof field === "number") + || (typeof field === "boolean") + || (field instanceof ObjectField) + || (field instanceof RefField); +} export type Field = number | string | boolean | ObjectField | RefField; export type Opt = T | undefined; export type FieldWaiting = T extends undefined ? never : Promise; @@ -48,6 +55,7 @@ export class Doc extends RefField { const doc = new Proxy(this, { set: setter, get: getter, + ownKeys: target => Object.keys(target.__fields), deleteProperty: () => { throw new Error("Currently properties can't be deleted from documents, assign to undefined instead"); }, defineProperty: () => { throw new Error("Currently properties can't be defined on documents using Object.defineProperty"); }, }); diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts index 649dfdc3e..4808d6e46 100644 --- a/src/new_fields/Types.ts +++ b/src/new_fields/Types.ts @@ -73,9 +73,9 @@ export function BoolCast(field: FieldResult, defaultVal: Opt = undefine type WithoutList = T extends List ? R[] : T; -export function FieldValue>(field: Opt | Promise>, defaultValue: U): WithoutList; -export function FieldValue(field: Opt | Promise>): Opt; -export function FieldValue(field: Opt | Promise>, defaultValue?: T): Opt { +export function FieldValue>(field: FieldResult, defaultValue: U): WithoutList; +export function FieldValue(field: FieldResult): Opt; +export function FieldValue(field: FieldResult, defaultValue?: T): Opt { return field instanceof Promise ? defaultValue : field; } -- cgit v1.2.3-70-g09d2