Skip to content

Commit

Permalink
Refactor generics names
Browse files Browse the repository at this point in the history
  • Loading branch information
yishn committed Mar 22, 2023
1 parent ee47140 commit 0353573
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions src/enum.ts
@@ -1,6 +1,6 @@
type NoUndefined<T> = Exclude<T, undefined | void>;

declare const enumDefinition: unique symbol;
declare const enumVariants: unique symbol;
declare const enumMutable: unique symbol;
const enumFactory = Symbol();

Expand Down Expand Up @@ -36,42 +36,43 @@ const enumFactory = Symbol();
* type Message<T> = Enum<MessageVariants<T>>;
* ```
*/
export type Enum<E> = Readonly<
(unknown extends E ? {}
export type Enum<V> = Readonly<
(unknown extends V
? {}
: {
[K in keyof E]:
& Record<K, NoUndefined<E[K]>>
& Partial<Record<Exclude<keyof Sanitize<E>, K>, never>>;
}[keyof Sanitize<E>]) & {
[enumDefinition]?: Sanitize<E>;
[K in keyof V]: Record<K, NoUndefined<V[K]>> &
Partial<Record<Exclude<keyof Sanitize<V>, K>, never>>;
}[keyof Sanitize<V>]) & {
[enumVariants]?: Sanitize<V>;
[enumMutable]?: boolean;
}
>;

type Sanitize<E> = Omit<
E,
type Sanitize<V> = Omit<
V,
| "_"
| {
[K in keyof E]: NoUndefined<E[K]> extends never ? K : never;
}[keyof E]
[K in keyof V]: NoUndefined<V[K]> extends never ? K : never;
}[keyof V]
>;

type EnumDefinition<T extends Enum<unknown>> = NoUndefined<
T[typeof enumDefinition]
type EnumVariants<E extends Enum<unknown>> = NoUndefined<
E[typeof enumVariants]
>;

type EnumFactory<E> = {
[K in keyof Sanitize<E>]: E[K] extends null ? () => Enum<E>
: (value: NoUndefined<E[K]>) => Enum<E>;
type EnumFactory<V> = {
[K in keyof Sanitize<V>]: V[K] extends null
? () => Enum<V>
: (value: NoUndefined<V[K]>) => Enum<V>;
};

type ExhaustiveMatcher<E> = {
[K in keyof Sanitize<E>]: (value: NoUndefined<Sanitize<E>[K]>) => unknown;
type ExhaustiveMatcher<V> = {
[K in keyof Sanitize<V>]: (value: NoUndefined<Sanitize<V>[K]>) => unknown;
};

type WildcardMatcher<E> = Partial<ExhaustiveMatcher<E>> & { _: () => unknown };
type WildcardMatcher<V> = Partial<ExhaustiveMatcher<V>> & { _: () => unknown };

export type Matcher<E> = ExhaustiveMatcher<E> | WildcardMatcher<E>;
export type Matcher<V> = ExhaustiveMatcher<V> | WildcardMatcher<V>;

export function Variant<T = null>(): T {
return undefined as never;
Expand All @@ -81,7 +82,7 @@ export class NonExhaustiveMatcherError extends Error {
constructor() {
super(
"Non-exhaustive matcher. To ensure all possible cases are covered, you " +
"can add a wildcard `_` match arm.",
"can add a wildcard `_` match arm."
);
}
}
Expand Down Expand Up @@ -120,21 +121,21 @@ export namespace Enum {
* const quit = Message().Quit(null);
* ```
*/
export function factory<E>(
Enum: (new () => E) & { [enumFactory]?: EnumFactory<E> },
): EnumFactory<E> {
export function factory<V>(
Enum: (new () => V) & { [enumFactory]?: EnumFactory<V> }
): EnumFactory<V> {
if (Enum[enumFactory] != null) return Enum[enumFactory]!;

const result: Partial<EnumFactory<E>> = {};
const result: Partial<EnumFactory<V>> = {};

for (const variant in new Enum()) {
// @ts-ignore
result[variant] = ((value: any) => {
return { [variant]: value ?? null } as Enum<E>;
return { [variant]: value ?? null } as Enum<V>;
}) as any;
}

return (Enum[enumFactory] = result as EnumFactory<E>);
return (Enum[enumFactory] = result as EnumFactory<V>);
}

/**
Expand Down Expand Up @@ -186,11 +187,11 @@ export namespace Enum {
* ```
*/
export function match<
T extends Enum<unknown>,
M extends Matcher<EnumDefinition<T>>,
E extends Enum<unknown>,
M extends Matcher<EnumVariants<E>>
>(
value: T,
matcher: M,
value: E,
matcher: M
): {
[K in keyof M]: M[K] extends (arg: any) => any ? ReturnType<M[K]> : never;
}[keyof M] {
Expand Down Expand Up @@ -229,7 +230,7 @@ export namespace Enum {
* // => { B: "Hello" }
* ```
*/
export function mutate<T extends Enum<unknown>>(value: T, other: T): void {
export function mutate<E extends Enum<unknown>>(value: E, other: E): void {
for (const variant in value) {
// @ts-ignore
delete value[variant];
Expand Down

0 comments on commit 0353573

Please sign in to comment.