Skip to content

Commit

Permalink
Extract declaration and helper types
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Aug 22, 2023
1 parent 179c962 commit 9385db8
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions lib/advanced-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// @ts-ignore Avoid strict dependency on 'pg'
// @ts-ignore Ignoring to avoid strict dependency on 'pg'
import type { Pool as PgPool, PoolClient as PgPoolClient } from 'pg';
import type { PluginDefinition } from 'plugin-importer';
import type { MigrationParams, UmzugStorage } from 'umzug';

import type {
AnyDeclaration,
AnyDeclarationType,
ValidDeclaration
} from './types/declaration-types.d.ts';
import type { PartialKeys } from './types/util-types.d.ts';

// What a dependency should provide
export interface UmzeptionDependency<T extends AnyUmzeptionContext = AnyUmzeptionContext> extends PluginDefinition {
glob: string[]
Expand Down Expand Up @@ -43,29 +50,21 @@ export abstract class UmzeptionStorage<T extends AnyUmzeptionContext> implements

// *** Context definitions ***

interface UmzeptionContextExtras {
value: unknown
}

export interface DefineUmzeptionContexts {
pg: UmzeptionContext<'pg', FastifyPostgresStyleDb>,
unknown: UmzeptionContext<'unknown', unknown>,
}

type ValidUmzeptionContexts = {
[key in keyof DefineUmzeptionContexts as (
DefineUmzeptionContexts[key] extends BaseUmzeptionContext
? Equal<key, DefineUmzeptionContexts[key]["type"]>
: never
)]: string extends key ? never : DefineUmzeptionContexts[key]
}

export type AnyUmzeptionContext = ValidUmzeptionContexts[keyof ValidUmzeptionContexts];
export type UmzeptionContextTypes = AnyUmzeptionContext["type"];
export type AnyUmzeptionContext = AnyDeclaration<DefineUmzeptionContexts, UmzeptionContextExtras>;
export type UmzeptionContextTypes = AnyDeclarationType<DefineUmzeptionContexts, UmzeptionContextExtras>

interface BaseUmzeptionContext {
type: string
value: unknown
}

export interface UmzeptionContext<T extends UmzeptionContextTypes, V> extends BaseUmzeptionContext {
type: string extends T ? never : (T extends string ? T : never);
export interface UmzeptionContext<T extends UmzeptionContextTypes, V>
extends ValidDeclaration<DefineUmzeptionContexts, UmzeptionContextExtras, T>
{
value: V
}

Expand All @@ -82,9 +81,3 @@ export type FastifyPostgresStyleDb = {
connect: PgPool["connect"];
transact: FastifyPostgresStyleTransact;
};

// *** Helpers ***

type PartialKeys<T, Keys extends keyof T> = Omit<T, Keys> & Partial<Pick<T, Keys>>;
type StringLiteral<T> = string extends T ? never : T;
type Equal<A, B> = A extends B ? A : never;

0 comments on commit 9385db8

Please sign in to comment.