supermacro/neverthrow

[Question] Typing Errors in interfaces

Opened this issue · 0 comments

I really like the idea of type-safe errors but still struggle on how to correctly define them in interfaces.

Let's say I have the following interface for an AI prompt template with a create() and validate() method. Both of these may produce different errors so I added generic type parameters for TCreateError and TValidateError. While this works, it of course scales with the amount of methods and gets pretty unwieldy.

Am I doing this wrong? Should I define the errors upfront (Result<Prompt<TSettings, TData>, CreateError>)? I wanted to give implementing classes the option to define multiple "sub-errors" to provide more fine-grained errors as a union.

export interface Template<
  TSettings extends AiModelSettings,
  TData extends PromptData,
  TParameters extends PromptParameters,
  TModelGeneration extends AiModelGeneration,
  TGeneration,
  TCreateError,
  TValidateError
> {
  readonly id: TemplateId
  readonly workspaceId: WorkspaceId
  readonly aiModelId: AiModelId
  readonly name: string
  readonly settings: TSettings
  readonly data: TData

  create(args: TParameters): Result<Prompt<TSettings, TData>, TCreateError>
  validate(generation: Generation<TModelGeneration>): Result<Generation<TGeneration>, TValidateError>
}