Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript support for useExperiment #30

Open
mr-piratman opened this issue Oct 5, 2021 · 3 comments
Open

typescript support for useExperiment #30

mr-piratman opened this issue Oct 5, 2021 · 3 comments

Comments

@mr-piratman
Copy link

Expected Behavior

Support of useExperiment when

yarn add -D @types/marvelapp___react-ab-test

Current Behavior

not Supporting of useExperiment when

yarn add -D @types/marvelapp___react-ab-test

I wrote a type definition for myself so if anyone wants it just let me know.
but I would happy to have it out of the box.

thanks 🍻

@mcccoleman
Copy link

Expected Behavior

Support of useExperiment when yarn add -D @types/marvelapp___react-ab-test

Current Behavior

not Supporting of useExperiment when yarn add -D @types/marvelapp___react-ab-test

I wrote a type definition for myself so if anyone wants it just let me know. but I would happy to have it out of the box.

thanks 🍻

@mr-piratman I would appreciate the type definition you used!

@mcccoleman
Copy link

Same issue here, useExperiment doesn't seem to be exported from the package.

@mr-piratman
Copy link
Author

eventually, I add

// Type definitions for @marvelapp/react-ab-test 3.1
// Project: https://github.com/marvelapp/react-ab-test#readme
// Definitions by: mtayllan <https://github.com/mtayllan>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="react" />

declare module '@marvelapp/react-ab-test' {
    export type ListenerCallback = (experimentName: string, variantName: string) => void;

    export interface Subscription {
        remove(): void;

        listener: ListenerCallback;
        eventType: string;
    }

    export namespace emitter {
        function emitWin(experimentName: string): void;

        function addActiveVariantListener(eventName: (string | ListenerCallback), callback?: ListenerCallback): Subscription;

        function addPlayListener(eventName: (string | ListenerCallback), callback?: ListenerCallback): Subscription;

        function addWinListener(eventName: (string | ListenerCallback), callback?: ListenerCallback): Subscription;

        function defineVariants(experimentName: string, variantNames: string[], variantWeights?: number[]): void;

        function setActiveVariant(experimentName: string, variantName: string): void;

        function getActiveVariant(experimentName: string): string;

        function calculateActiveVariant(experimentName: string, userIdentifier?: string, defaultVariantName?: string): string;

        function getSortedVariants(experimentName: string): string[];

        function setCustomDistributionAlgorithm(customAlgorithm: (experimentName: string, userIdentifier: string, defaultVariantName?: string) => void): void;
    }

    export interface ExperimentProps {
        name: string;
        defaultVariantName?: string;
        userIdentifier?: string;
        children: React.ReactNode;
    }

    export function Experiment({
                                   name, defaultVariantName, userIdentifier, children,
                               }: ExperimentProps): JSX.Element;

    export function Variant({
                                name,
                                children,
                            }: { name: string, children: React.ReactNode }): JSX.Element;

    export namespace experimentDebugger {
        function setDebuggerAvailable(isAvailable: boolean): void;

        function enable(): void;

        function disable(): void;
    }

    export namespace mixpanelHelper {
        function enable(): void;

        function disable(): void;
    }

    export namespace segmentHelper {
        function enable(): void;

        function disable(): void;
    }

    export function emitWin(): void;

    export function selectVariant<T extends Record<string, any>>(variants: T): T[keyof T];

    export function useExperiment(key: string): { emitWin: typeof emitWin, selectVariant: typeof selectVariant }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants