Inline definition of Record<> to uncollapse Value<T>

This commit is contained in:
Tony Garnock-Jones 2021-03-01 09:19:32 +01:00
parent 481f866ada
commit 1268c4f9bd
1 changed files with 23 additions and 3 deletions

View File

@ -15,9 +15,29 @@ export * from './dictionary';
export type DefaultPointer = object;
export type Value<T extends object = DefaultPointer> = Atom | Compound<T> | T | Annotated<T>;
export type Atom = boolean | SingleFloat | DoubleFloat | number | string | Bytes | symbol;
export type Compound<T extends object = DefaultPointer> = Record<any, any, T> | Array<Value<T>> | Set<T> | Dictionary<Value<T>, T>;
export type Value<T extends object = DefaultPointer> =
| Atom
| Compound<T>
| T
| Annotated<T>;
export type Atom =
| boolean
| SingleFloat
| DoubleFloat
| number
| string
| Bytes
| symbol;
export type Compound<T extends object = DefaultPointer> =
| (Array<Value<T>> | [Value<T>]) & { label: Value<T> }
// ^ expanded from definition of Record<> in record.ts,
// because if we use Record<Value<T>, Tuple<Value<T>>, T>,
// TypeScript currently complains about circular use of Value<T>,
// and if we use Record<any, any, T>, it accepts it but collapses
// Value<T> to any.
| Array<Value<T>>
| Set<T>
| Dictionary<Value<T>, T>;
export function fromJS<T extends object = DefaultPointer>(x: any): Value<T> {
switch (typeof x) {