From ad74425473aa76e718ea3b35d38b5f3b7ca358e1 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Mon, 6 May 2019 19:59:04 -0400 Subject: Changed some types to be more correct for lists --- src/new_fields/Doc.ts | 8 +++++--- src/new_fields/Types.ts | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src') 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; +export function DocListCast(field: FieldResult, defaultValue: Doc[]): Promise; +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 | ListSpec> = 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 List ? R[] : T; +type WithoutList = T extends List ? (R extends RefField ? (R | Promise)[] : R[]) : T; export function FieldValue>(field: FieldResult, defaultValue: U): WithoutList; export function FieldValue(field: FieldResult): Opt; -- cgit v1.2.3-70-g09d2