diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/new_fields/Doc.ts | 8 | ||||
-rw-r--r-- | src/new_fields/Types.ts | 3 |
2 files changed, 7 insertions, 4 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index 38c220bc8..afcf71fc9 100644 --- a/src/new_fields/Doc.ts +++ b/src/new_fields/Doc.ts @@ -29,9 +29,11 @@ const SelfProxy = Symbol("SelfProxy"); export const WidthSym = Symbol("Width"); export const HeightSym = Symbol("Height"); -export function DocListCast(field: FieldResult) { - const list = Cast(field, listSpec(Doc)) - return list ? Promise.all(list) : Promise.resolve(undefined); +export function DocListCast(field: FieldResult): Promise<Doc[] | undefined>; +export function DocListCast(field: FieldResult, defaultValue: Doc[]): Promise<Doc[]>; +export function DocListCast(field: FieldResult, defaultValue?: Doc[]) { + const list = Cast(field, listSpec(Doc)); + return list ? Promise.all(list) : Promise.resolve(defaultValue); } @Deserializable("doc").withFields(["id"]) diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts index c07d38786..4b4c58eb8 100644 --- a/src/new_fields/Types.ts +++ b/src/new_fields/Types.ts @@ -1,5 +1,6 @@ import { Field, Opt, FieldResult, Doc } from "./Doc"; import { List } from "./List"; +import { RefField } from "./RefField"; export type ToType<T extends ToConstructor<Field> | ListSpec<Field>> = T extends "string" ? string : @@ -71,7 +72,7 @@ export function BoolCast(field: FieldResult, defaultVal: boolean | null = null) return Cast(field, "boolean", defaultVal); } -type WithoutList<T extends Field> = T extends List<infer R> ? R[] : T; +type WithoutList<T extends Field> = T extends List<infer R> ? (R extends RefField ? (R | Promise<R>)[] : R[]) : T; export function FieldValue<T extends Field, U extends WithoutList<T>>(field: FieldResult<T>, defaultValue: U): WithoutList<T>; export function FieldValue<T extends Field>(field: FieldResult<T>): Opt<T>; |