From 6b9a3d2ee4ec49f38f02106e3aa264946b7cdc93 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 13 Jan 2022 14:49:05 +0530 Subject: refactor: type + constructors refactor --- src/utils.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/utils.ts') diff --git a/src/utils.ts b/src/utils.ts index 15d5fb0..66c4c53 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,19 +3,22 @@ export const eq = (b: T): boolean => a === b -type TagValue = T extends Tag ? V : never; +type TagValue = T extends Tag ? V : never export const match = - > - (pattern: { [key in T['tag'] | '_']?: (v: TagValue) => R }) => - (tag: T): R => - (pattern[tag.tag] || pattern._ as any)(tag.value) + >(pattern: { + [key in T['tag'] | '_']?: (v: TagValue) => R + }) => + (tag: T): R => + (pattern[tag.tag] || (pattern._ as any))(tag.value) type Tag = { tag: N; value: V } export type Union = { [N in keyof T]: Tag }[keyof T] -export const constructors = >(): { - [N in keyof T]: (value: T[N]) => Union // Tag +export const constructors = >(): { + [N in T['tag']]: TagValue extends null|never + ? (value?: null | never) => T + : (value: TagValue) => T } => new Proxy( {}, @@ -27,4 +30,3 @@ export const constructors = >(): { ) as any export const jlog = (x: any) => console.log(JSON.stringify(x, null, 2)) - -- cgit v1.3.1