Fix types... Jesus

This commit is contained in:
Alex Gleason 2023-03-24 22:33:12 -05:00
parent 84ad093409
commit 2330349ad6
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

View File

@ -1,7 +1,9 @@
import { InputMessage, OutputMessage, Policy } from './types.ts'; import { InputMessage, OutputMessage, Policy } from './types.ts';
/** A policy function with opts to run it with. Used by the pipeline. */ /** A policy function with opts to run it with. Used by the pipeline. */
type PolicyTuple<Opts = unknown> = [policy: Policy<Opts>, opts?: Opts]; type PolicyTuple<P extends Policy = Policy> = [policy: P, opts?: InferPolicyOpts<P>];
/** Infer opts from the policy. */
type InferPolicyOpts<P> = P extends Policy<infer Opts> ? Opts : never;
/** Helper type for proper type inference of PolicyTuples in the pipeline. */ /** Helper type for proper type inference of PolicyTuples in the pipeline. */
// https://stackoverflow.com/a/75806165 // https://stackoverflow.com/a/75806165
@ -28,7 +30,7 @@ async function pipeline<T extends any[]>(msg: InputMessage, policies: [...Policy
} }
/** Coerce item into a tuple if it isn't already. */ /** Coerce item into a tuple if it isn't already. */
function toTuple<T>(item: PolicyTuple<T> | Policy<T>): PolicyTuple<T> { function toTuple<P extends Policy>(item: PolicyTuple<P> | P): PolicyTuple<P> {
return typeof item === 'function' ? [item] : item; return typeof item === 'function' ? [item] : item;
} }