Effect CommunityEC
Effect Community3y ago
13 replies
bigpopakap

Improving Refinement.compose Functions

I think the Refinement.compose functions could be improved. As it is right now, they lose any previous type information

Example:
// Type 'Refinement<unknown, Equal>' is not assignable to type '(thing: unknown) => thing is Equal & string'.
// Type predicate 'a is Equal' is not assignable to 'thing is Equal & string'.
//   Type 'Equal' is not assignable to type 'Equal & string'.
//     Type 'Equal' is not assignable to type 'string'.ts(2322)
const isNotNullableAndEqual: (thing: unknown) => thing is EQ.Equal & string =
    pipe(
        P.isString,
        P.compose(EQ.isEqual),
    );

This seems to be happening because EQ.isEqual has lost the refinement to string

The error is fixed if I update isEqual as follows:
// Just temporarily simulating a new signature for isEqual
declare const isEqual_NEW: <A>(a: A) => a is A & EQ.Equal;

const isNotNullableAndEqual: (thing: unknown) => thing is EQ.Equal & string =
    pipe(
        P.isString,
        P.compose(isEqual_NEW),
    );


Is there some other way around this? Does this make sense as an update to the library?
Was this page helpful?