Effect CommunityEC
Effect Community3y ago
57 replies
bigpopakap

Adding Built-in Refinements to `Data.case`

Looks like Data.case doesn't have any built-in refinements. Could that be added? I imagine it should be pretty easy:

export interface First extends D.Case {
    _tag: "First";
    readonly first: number;
};

export const First = D.case<First>();

export interface Second extends D.Case {
    _tag: "Second";
    readonly second: string;
};

export const Second = D.case<Second>();

export type Thing = First | Second;

// TODO can Data.Case give this for free?
export const isFirst: P.Refinement<Thing, First> =
    (thing): thing is First =>
        pipe(
            M.value(thing),
            M.tag('First', constTrue),
            M.tag('Second', constFalse),
            M.exhaustive,
        );


I'd hope for something lke:
export const isFirst = D.isCase<First>();
Was this page helpful?