Effect CommunityEC
Effect Community2mo ago
5 replies
Toby

Type of HttpApiError.NotFound

Anyone know how to tell Typescript that new HttpApiError.NotFound() is a NotFound, not Effect.Effect<never, NotFound>? (without casting). I've got this weird compiler error:

const mapError = (value: string): HttpApiError.NotFound | HttpApiError.BadRequest => pipe(
  Match.value(value),
  Match.withReturnType<HttpApiError.NotFound | HttpApiError.BadRequest>(),
  Match.when("missing", () => new HttpApiError.NotFound()),
  Match.orElse(() => new HttpApiError.BadRequest())
)

Type 'Effect<never, BadRequest | NotFound, never>' is not assignable to type 'BadRequest | NotFound'

This works but is kinda nasty:
const mapError = (value: string): HttpApiError.NotFound | HttpApiError.BadRequest => pipe(
  Match.value(value),
  Match.withReturnType<HttpApiError.NotFound | HttpApiError.BadRequest>(),
  Match.when("missing", () => new HttpApiError.NotFound()),
  Match.orElse(() => new HttpApiError.BadRequest())
) as HttpApiError.NotFound | HttpApiError.BadRequest
Was this page helpful?