Effect CommunityEC
Effect Community2y ago
6 replies
BinaryArtifex

Maintaining Optional Type with Default Value in TypeScript

is there a way to keep the optional type even when a default value is provided? Im trying to be able to pass a fallback message for my http errors and only override them on a case by case basis. however it appears i can't both have a default value for
message
and still keep its optional status...

import * as S from "@effect/schema/Schema";
import { fallbackMessage } from "./http-error.const";

export class HttpResponseError extends S.TaggedError<HttpResponseError>()("HttpResponseError", {
  cause: S.optional(S.unknown),
  message: S.optional(S.string, {
    default: () => fallbackMessage.HttpResponseError,
    nullable: true,
  }),
}) {
  static is<A>(other: A | HttpResponseError): other is HttpResponseError {
    return other instanceof HttpResponseError;
  }

  static withCause(message = fallbackMessage.HttpResponseError) {
    return (cause: unknown) => new HttpResponseError({ message, cause });
  }
}


ideally id like to support all these apis for each error....is this possible?

new HttpResponseError({ cause: cause, message: "error" });
new HttpResponseError({ message: "error" });
new HttpResponseError({ cause: cause });
new HttpResponseError();
Was this page helpful?