diff options
| author | Tyler Schicke <tyler_schicke@brown.edu> | 2019-04-20 03:29:35 -0400 |
|---|---|---|
| committer | Tyler Schicke <tyler_schicke@brown.edu> | 2019-04-20 03:29:35 -0400 |
| commit | 1eb965a5d9c8aaebf1970bc645edecfb7017b601 (patch) | |
| tree | 67f628de6459603f462b53cd59d78a548b5c59ae /src/new_fields/Schema.ts | |
| parent | 4bf22b6095cee078403e4ac11743e7aaa557956c (diff) | |
Made the switch in a couple more classes
Diffstat (limited to 'src/new_fields/Schema.ts')
| -rw-r--r-- | src/new_fields/Schema.ts | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/new_fields/Schema.ts b/src/new_fields/Schema.ts index 1607d4c15..1d1f56844 100644 --- a/src/new_fields/Schema.ts +++ b/src/new_fields/Schema.ts @@ -1,8 +1,26 @@ -import { Interface, ToInterface, Cast, FieldCtor, ToConstructor } from "./Types"; +import { Interface, ToInterface, Cast, FieldCtor, ToConstructor, HasTail, Head, Tail } from "./Types"; import { Doc } from "./Doc"; -export type makeInterface<T extends Interface, U extends Doc = Doc> = Partial<ToInterface<T>> & U; -export function makeInterface<T extends Interface, U extends Doc>(schema: T): (doc: U) => makeInterface<T, U> { +type All<T extends any[], U extends Doc> = { + 1: makeInterface<Head<T>, U> & All<Tail<T>, U>, + 0: makeInterface<Head<T>, U> +}[HasTail<T> extends true ? 1 : 0]; + +type AllToInterface<T extends any[]> = { + 1: ToInterface<Head<T>> & AllToInterface<Tail<T>>, + 0: ToInterface<Head<T>> +}[HasTail<T> extends true ? 1 : 0]; + +export type makeInterface<T extends Interface[], U extends Doc = Doc> = Partial<AllToInterface<T>> & U; +// export function makeInterface<T extends Interface[], U extends Doc>(schemas: T): (doc: U) => All<T, U>; +// export function makeInterface<T extends Interface, U extends Doc>(schema: T): (doc: U) => makeInterface<T, U>; +export function makeInterface<T extends Interface[], U extends Doc>(schemas: T): (doc: U) => All<T, U> { + let schema: Interface = {}; + for (const s of schemas) { + for (const key in s) { + schema[key] = s[key]; + } + } return function (doc: any) { return new Proxy(doc, { get(target, prop) { |
