Skip to content

Commit

Permalink
fix: adding error definition for getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Dec 21, 2024
1 parent 58d249e commit b8a65a7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './guards';
import {
AnyIterable,
AssertError,
Class,
Func,
KeysOfType,
Expand All @@ -29,8 +30,6 @@ import {
Sizeable,
} from './types';

type AssertError = string | Func<[], unknown>;

function throwError(errorCallOrMessage: AssertError): never {
throw typeof errorCallOrMessage === 'function'
? errorCallOrMessage()
Expand Down
11 changes: 7 additions & 4 deletions src/getters.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { assertAndGet, assertAndGetProperty } from './asserts';
import { isNonNullish, isString } from './guards';
import { KeysOfType, Nullable, ObjectKeyType } from './types';
import { AssertError, KeysOfType, Nullable, ObjectKeyType } from './types';

export function assertAndGetNonNullish<T>(value: Nullable<T>): NonNullable<T> {
return assertAndGet(value, isNonNullish);
}
export function getDefinedProperty<T extends object, K extends keyof T>(
obj: T,
prop: K,
errorCallOrMessage?: AssertError,
): NonNullable<T[K]> {
return assertAndGetProperty(obj, prop, isNonNullish);
return assertAndGetProperty(obj, prop, isNonNullish, errorCallOrMessage);
}

export function assertAndGetStringProperty<
T extends object,
K extends KeysOfType<T, Nullable<string>>,
>(obj: T, prop: K): string;
>(obj: T, prop: K, errorCallOrMessage?: AssertError): string;
export function assertAndGetStringProperty<T extends object, K extends keyof T>(
obj: T,
prop: K,
errorCallOrMessage?: AssertError,
): string;
export function assertAndGetStringProperty<K extends keyof ObjectKeyType>(
obj: any,
prop: K,
errorCallOrMessage?: AssertError,
): string {
return assertAndGetProperty(obj, prop, isString);
return assertAndGetProperty(obj, prop, isString, errorCallOrMessage);
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ export type RecursiveOmit<T, K extends ObjectKeyType> = T extends unknown[]
},
K
>;
export type AssertError = string | Func<[], unknown>;

0 comments on commit b8a65a7

Please sign in to comment.