36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
export type Type =
|
|
| { kind: 'union', variants: VariantMap } // zero: never
|
|
| SimpleType
|
|
|
|
export type SimpleType = AtomicType | CompoundType
|
|
export type FieldType = AtomicType | CollectionType;
|
|
|
|
export type AtomicType =
|
|
| { kind: 'unit' }
|
|
| { kind: 'ref', typeName: string } // also for base types
|
|
|
|
export type CompoundType =
|
|
| CollectionType
|
|
| { kind: 'record', fields: FieldMap }
|
|
|
|
export type CollectionType =
|
|
| { kind: 'array', type: AtomicType }
|
|
| { kind: 'set', type: AtomicType }
|
|
| { kind: 'dictionary', key: AtomicType, value: AtomicType }
|
|
|
|
export type VariantMap = Map<string, SimpleType>;
|
|
export type FieldMap = Map<string, FieldType>;
|
|
|
|
export namespace Type {
|
|
export const union = (variants: VariantMap): Type => ({ kind: 'union', variants });
|
|
export const unit = (): AtomicType => ({ kind: 'unit' });
|
|
export const ref = (typeName: string): AtomicType => ({ kind: 'ref', typeName });
|
|
export const record = (fields: FieldMap): CompoundType => ({ kind: 'record', fields });
|
|
export const array = (type: AtomicType): CollectionType => ({ kind: 'array', type });
|
|
export const set = (type: AtomicType): CollectionType => ({ kind: 'set', type });
|
|
export const dictionary = (key: AtomicType, value: AtomicType): CollectionType => (
|
|
{ kind: 'dictionary', key, value });
|
|
}
|
|
|
|
export const ANY_TYPE: AtomicType = Type.ref('_val');
|