Skip to content

Commit

Permalink
fix: throw helpers return type (never)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Apr 9, 2022
1 parent e9ac1a8 commit 87c1fa4
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ export type NewPojoError<TErrorTypes extends PojoErrorTypes> = <
...args: [...Parameters<TErrorTypes[TType]>]
) => PojoErrorInstance<TErrorTypes, TType>;

export type ThrowPojoError<TErrorTypes extends PojoErrorTypes> =
NewPojoError<TErrorTypes>;
export type ThrowPojoError<TErrorTypes extends PojoErrorTypes> = <
TType extends keyof TErrorTypes,
>(
type: TType,
...args: [...Parameters<TErrorTypes[TType]>]
) => never;

export type NewPojoErrorWithCause<TErrorTypes extends PojoErrorTypes> = <
TType extends keyof TErrorTypes,
Expand All @@ -46,16 +50,21 @@ export type NewPojoErrorWithCause<TErrorTypes extends PojoErrorTypes> = <
...args: [...Parameters<TErrorTypes[TType]>]
) => PojoErrorInstance<TErrorTypes, TType>;

export type ThrowPojoErrorWithCause<TErrorTypes extends PojoErrorTypes> =
NewPojoErrorWithCause<TErrorTypes>;
export type ThrowPojoErrorWithCause<TErrorTypes extends PojoErrorTypes> = <
TType extends keyof TErrorTypes,
>(
cause: Error,
type: TType,
...args: [...Parameters<TErrorTypes[TType]>]
) => never;

export type PojoFactory<TErrorTypes extends PojoErrorTypes> = {
type: Unwrap<PojoEnum<TErrorTypes>>;
errors: TErrorTypes;
new: NewPojoError<TErrorTypes>;
newFrom: NewPojoErrorWithCause<TErrorTypes>;
throw: ThrowPojoError<TErrorTypes>;
throwFrom: NewPojoErrorWithCause<TErrorTypes>;
throwFrom: ThrowPojoErrorWithCause<TErrorTypes>;
is: <TType extends keyof TErrorTypes>(
type: TType,
error: unknown,
Expand Down Expand Up @@ -197,15 +206,15 @@ export function factory<TErrorTypes extends PojoErrorTypes>(
function throwError<TType extends keyof TErrorTypes>(
type: TType,
...args: [...Parameters<TErrorTypes[TType]>]
): PojoErrorInstance<TErrorTypes, TType> {
): never {
throw newError(type, ...args);
}

function throwFromError<TType extends keyof TErrorTypes>(
cause: Error,
type: TType,
...args: [...Parameters<TErrorTypes[TType]>]
): PojoErrorInstance<TErrorTypes, TType> {
): never {
throw newFromError(cause, type, ...args);
}

Expand Down

0 comments on commit 87c1fa4

Please sign in to comment.