Effect CommunityEC
Effect Community2y ago
4 replies
Sly

Using Utility Function for Parsing and Error Handling

I've been using a utility function such as

import * as Schema from '@effect/schema/Schema';
import { formatErrors } from '@effect/schema/TreeFormatter';
import { Data, Effect } from 'effect';

export class DecodeError extends Data.TaggedError('DecodeError')<{
    message?: string;
}> {}

export function parse<_, A>(schema: Schema.Schema<_, A>) {
    return (input: unknown) =>
        Schema.parse(schema)(input, { errors: 'all' }).pipe(
            Effect.catchAll(
                (error) => new DecodeError({ message: formatErrors(error.errors) }),
            ),
        );
}


Until now. With the latest update I switched to:
import * as Schema from '@effect/schema/Schema';
import { formatError } from '@effect/schema/TreeFormatter';
import { Data, Effect } from 'effect';

export class DecodeError extends Data.TaggedError('DecodeError')<{
    message?: string;
}> {}

export function parse<_, A>(schema: Schema.Schema<_, A>) {
    return (input: unknown) =>
        Schema.decodeUnknown(schema)(input, { errors: 'all' }).pipe(
            Effect.catchAll(
                (error) => new DecodeError({ message: formatError(error) }),
            ),
        );
}


Yet I now see a lot of TS2345: Argument of type 'Schema<never, string | boolean, boolean>' is not assignable to parameter of type 'Schema<never, string | boolean, string | boolean>.

What's the current recommended approach for such a utility?
Was this page helpful?