Skip to content

Commit

Permalink
fix(type): fix ValidateAllEventsEnumAreImplemented
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlabo committed Apr 21, 2024
1 parent ebf4e71 commit 9c0c7f9
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/private/validate-all-events-implemented.type.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { PluginListenerHandle } from '@capacitor/core';


type Contra<T> = (x: T) => void
type UnwrapContra<T> = [T] extends [Contra<infer S>] ? S : never
type UnionToIntersection<U> = UnwrapContra<U extends any ? Contra<U> : never>

type Overload<T> =
(eventName: T, listenerFunc: (...args: any[]) => any) => PluginListenerHandle
type OverloadUnionForEnum<T> = T extends any ? Overload<T> : never

type OverloadUnion<T> = OverloadUnionForEnum<T>
// Overload<RewardAdPluginEvents.FailedToLoad> | Overload<RewardAdPluginEvents.Dismissed> | Overload<RewardAdPluginEvents.Rewarded>

type Overloads<T> = UnionToIntersection<OverloadUnion<T>>

export type ValidateAllEventsEnumAreImplemented<TEventsEnum, TDefinitionInterface extends { addListener: Overloads<TEventsEnum> }> = TDefinitionInterface;
import type { PluginListenerHandle } from '@capacitor/core';

type Contra<T> = (x: T) => void;
type UnwrapContra<T> = [T] extends [Contra<infer S>] ? S : never;
type UnionToIntersection<U> = UnwrapContra<U extends any ? Contra<U> : never>;

type Overload<T> = (
eventName: T,
listenerFunc: (...args: any[]) => any,
) => Promise<PluginListenerHandle>;
type OverloadUnionForEnum<T> = T extends any ? Overload<T> : never;

type OverloadUnion<T> = OverloadUnionForEnum<T>;
// Overload<RewardAdPluginEvents.FailedToLoad> | Overload<RewardAdPluginEvents.Dismissed> | Overload<RewardAdPluginEvents.Rewarded>

type Overloads<T> = UnionToIntersection<OverloadUnion<T>>;

export type ValidateAllEventsEnumAreImplemented<
TEventsEnum,
TDefinitionInterface extends { addListener: Overloads<TEventsEnum> },
> = TDefinitionInterface;

0 comments on commit 9c0c7f9

Please sign in to comment.