Skip to content

Commit

Permalink
feat: allow specifying error message function
Browse files Browse the repository at this point in the history
So that you can provide a custom error message that includes the value
  • Loading branch information
codeandcats committed Jul 6, 2024
1 parent 5259e13 commit 2cba510
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function assertNever(value: never, errorMessage?: string): never
export function assertNever(value: never, getErrorMessage?: (value: unknown) => string): never
export function assertNever(value: never, noThrow?: boolean): never

/**
Expand Down Expand Up @@ -26,11 +27,15 @@ export function assertNever(value: never, noThrow?: boolean): never
* return assertNever(arg);
* }
*/
export function assertNever(value: never, errorMessageOrNoThrow?: string | boolean): never {
export function assertNever(value: never, errorMessageOrNoThrow?: string | ((value: unknown) => string) | boolean): never {
if (typeof errorMessageOrNoThrow === 'string') {
throw new Error(errorMessageOrNoThrow)
}

if (typeof errorMessageOrNoThrow === 'function') {
throw new Error(errorMessageOrNoThrow(value))
}

if (errorMessageOrNoThrow) {
return value
}
Expand Down

0 comments on commit 2cba510

Please sign in to comment.