Effect CommunityEC
Effect Community3y ago
9 replies
lildesert

Issue with Inferred Return Type in Effect Function

Hi there,
So I'm trying to use Effect to write a function, but the inferred return type is not what I expect.

In the first version, I don't specify it :
const validateRequestSignature = (
  requestBody: string,
  signatureHeader: string | null
) => {
  if (!signatureHeader) {
    return Effect.fail({
      message: 'Missing webhook signature header',
    } as const);
  }
  const signature = 'secret' + requestBody;
  const hashedSignature = crypto
    .createHash('sha256')
    .update(signature)
    .digest('hex');
  return hashedSignature === signatureHeader
    ? Effect.succeedNone()
    : Effect.fail({ message: 'Invalid webhook signature' } as const);
};


The inferred return type is
Effect.Effect<never, {
  readonly message: "Missing webhook signature header";
}, never> | Effect.Effect<never, never, Option.Option<never>> | Effect.Effect<never, {
  readonly message: "Invalid webhook signature";
}, never>


In the second version, I specify the return type and the compiler seems okay with it
const validateRequestSignature = (
  requestBody: string,
  signatureHeader: string | null
): Effect.Effect<
  never,
  {
    readonly message:
      | 'Invalid webhook signature'
      | 'Missing webhook signature header';
  },
  Option.Option<never>
> => {
  if (!signatureHeader) {
    return Effect.fail({
      message: 'Missing webhook signature header',
    } as const);
  }
  const signature = 'secret' + requestBody;
  const hashedSignature = crypto
    .createHash('sha256')
    .update(signature)
    .digest('hex');
  return hashedSignature === signatureHeader
    ? Effect.succeedNone()
    : Effect.fail({ message: 'Invalid webhook signature' } as const);
};


Is there a way to have this result without specifying the return type?
Thanks!
Was this page helpful?