Skip to content

Commit 994f163

Browse files
committed
refactor(types): AnyMethodGuards helper
1 parent 9394e3f commit 994f163

1 file changed

Lines changed: 25 additions & 29 deletions

File tree

packages/patterns/src/types.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,20 @@ export type PatternMatchers = {
652652
*/
653653
export type DefaultGuardType = undefined | 'passable' | 'raw';
654654

655+
/**
656+
* `MethodGuard` with every type parameter set to `any`, bypassing the
657+
* parameter defaults (e.g. `RetGuard extends SyncValueGuard = SyncValueGuard`).
658+
* See {@link MakeInterfaceGuardGeneral} for why the defaults are wrong here.
659+
*/
660+
export type AnyMethodGuard = MethodGuard<any, any, any, any, any>;
661+
662+
/** Record of method guards keyed by method name. */
663+
export type AnyMethodGuards = Record<PropertyKey, AnyMethodGuard>;
664+
655665
/**
656666
* Overload for strictly-typed interface guards (no sloppy mode).
657667
*/
658-
export type MakeInterfaceGuardStrict = <
659-
M extends Record<PropertyKey, MethodGuard<any, any, any, any, any>>,
660-
>(
668+
export type MakeInterfaceGuardStrict = <M extends AnyMethodGuards>(
661669
interfaceName: string,
662670
methodGuards: M,
663671
options: {
@@ -688,20 +696,17 @@ export type MakeInterfaceGuardSloppy = (
688696
/**
689697
* General overload for interface guards.
690698
*
691-
* The constraint uses `MethodGuard<any, any, any, any, any>` (all type
692-
* parameters set to `any`) rather than the bare `MethodGuard` (which
693-
* defaults its parameters to their constraint, e.g. `RetGuard extends
694-
* SyncValueGuard = SyncValueGuard`). Without this, TS contextually types
695-
* inline expressions like `M.call().returns(M.promise())` against
696-
* `SyncValueGuard`, which causes `MatcherOf<'promise', any>`'s `Payload`
697-
* type parameter to drift from `any` to a non-canonical unknown form
698-
* (`void | RawGuardPayload | null`) — breaking downstream
699-
* `TypeFromPattern` inference whose `unknown extends Payload` check
700-
* doesn't recognize that form as `unknown`.
701-
*/
702-
export type MakeInterfaceGuardGeneral = <
703-
M extends Record<PropertyKey, MethodGuard<any, any, any, any, any>>,
704-
>(
699+
* The constraint uses {@link AnyMethodGuard} (all type parameters set to
700+
* `any`) rather than the bare `MethodGuard` (which defaults its parameters
701+
* to their constraint, e.g. `RetGuard extends SyncValueGuard =
702+
* SyncValueGuard`). Without this, TS contextually types inline expressions
703+
* like `M.call().returns(M.promise())` against `SyncValueGuard`, which
704+
* causes `MatcherOf<'promise', any>`'s `Payload` type parameter to drift
705+
* from `any` to a non-canonical unknown form (`void | RawGuardPayload |
706+
* null`) — breaking downstream `TypeFromPattern` inference whose `unknown
707+
* extends Payload` check doesn't recognize that form as `unknown`.
708+
*/
709+
export type MakeInterfaceGuardGeneral = <M extends AnyMethodGuards>(
705710
interfaceName: string,
706711
methodGuards: M,
707712
options?: {
@@ -782,12 +787,7 @@ export type Method = (...args: any[]) => any;
782787
/**
783788
* Payload for an interface guard definition.
784789
*/
785-
export type InterfaceGuardPayload<
786-
T extends Record<PropertyKey, MethodGuard<any, any, any, any, any>> = Record<
787-
PropertyKey,
788-
MethodGuard<any, any, any, any, any>
789-
>,
790-
> = {
790+
export type InterfaceGuardPayload<T extends AnyMethodGuards = AnyMethodGuards> = {
791791
interfaceName: string;
792792
methodGuards: Omit<T, symbol> &
793793
Partial<{ [K in Extract<keyof T, symbol>]: never }>;
@@ -831,12 +831,8 @@ export type InterfaceGuardPayload<
831831
* await stringP; // => "42"
832832
* ```
833833
*/
834-
export type InterfaceGuard<
835-
T extends Record<PropertyKey, MethodGuard<any, any, any, any, any>> = Record<
836-
PropertyKey,
837-
MethodGuard<any, any, any, any, any>
838-
>,
839-
> = CopyTagged<'guard:interfaceGuard', InterfaceGuardPayload<T>>;
834+
export type InterfaceGuard<T extends AnyMethodGuards = AnyMethodGuards> =
835+
CopyTagged<'guard:interfaceGuard', InterfaceGuardPayload<T>>;
840836

841837
/**
842838
* A method name and parameter/return signature like:

0 commit comments

Comments
 (0)