Effect CommunityEC
Effect Community3y ago
4 replies
goofysystem

Using Effect with Effect Schema together

Hello. I think Effect that is being returned by S.parseEffect is slightly different or maybe I'm using it wrong :p

const FooSchema = S.struct({
    foo: S.string,
})

 const checkFooWithGenerator = (input: unknown) =>  Effect.gen(function* ($){
     const validFoo = yield* $(S.parseEffect(FooSchema)(input));    
}) 

const checkFooWithPipe = (input: unknown) => pipe(
    input,
    S.parseEffect(FooSchema),
    Effect.map(foo => foo)
)  

Type error for checkFooWithGenerator is:

Argument of type 'Effect<never, ParseError, { readonly foo: string; }>' is not assignable to parameter of type 'Effect<unknown, unknown, unknown>'.
     Type 'Effect<never, ParseError, { readonly foo: string; }>' is missing the following properties from type 'Effect<unknown, unknown, unknown>': [EffectTypeId], [symbol], [symbol], pipe [2345]


Type error for checkFooWithPipe is:
Argument of type '<R, E>(self: Effect<R, E, unknown>) => Effect<R, E, unknown>' is not assignable to parameter of type '(b: Effect<never, ParseError, { readonly foo: string; }>) => Effect<unknown, unknown, unknown>'.
     Types of parameters 'self' and 'b' are incompatible.
       Type 'Effect<never, ParseError, { readonly foo: string; }>' is not assignable to type 'Effect<unknown, unknown, unknown>'. [2345]


I am coming from using fp-ts and @effect/schema like this:

const FooSchema = S.struct({
    foo: S.string,
})

const checkFoo = (input: unknown) => pipe(
input,
S.parseEither(FooSchema),
E.mapLeft(errs => new ValidationFailed(errs)),
E.map(foo => foo)
)
Was this page helpful?