Skip to content

Commit

Permalink
♻️ Refactor(TMyError)!: add Generic to TMyError and to other types re…
Browse files Browse the repository at this point in the history
…ferenced with
  • Loading branch information
INeedJobToStartWork committed Feb 9, 2024
1 parent cd73aa8 commit 10fe733
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/tests/componentConcept/readFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const readFileHandler = {
if (name) return [{ code: "123" }, true];
return [123, false];
}
} as const satisfies TMyHandler<typeof ErrorList>;
} as const satisfies TMyHandler<NonNullable<unknown>, typeof ErrorList>;

export const readFile = (path: string): TFunctionReturn<string> => {
const finalPath = join(__dirname, path);
Expand Down
14 changes: 10 additions & 4 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type ErrorTypesCatched =
| SyntaxErrorConstructor
| TypeErrorConstructor;

export type TMyError = {
export type TMyError<T = NonNullable<unknown>> = T & {
code?: number | string;
message?: {
user?: string;
Expand All @@ -28,10 +28,16 @@ export type TMyError = {
};

export type TDataReturn<T> = [T, false];
export type TErrorReturned = [TMyError, true];
export type TErrorReturned<CustomError = NonNullable<unknown>> = [TMyError<CustomError>, true];
export type TFunctionReturn<T> = Prettify<TDataReturn<T>> | Prettify<TErrorReturned>;

export type TMyErrorList = Record<string, Required<TMyError>>;
export type TMyHandler<T extends TMyErrorList> = Partial<{
export type TMyErrorList<CustomError = NonNullable<unknown>> = Record<string, Required<TMyError<CustomError>>>;
export type TMyHandler<
CustomError,
T extends Record<number | string, { code: string & unknown }> & TMyErrorList<CustomError>
> = Partial<{
[K in T[keyof T]["code"]]: (...args: K[]) => TFunctionReturn<unknown>;
}>;
// export type TMyHandler<,T extends TMyErrorList<CustomError>> = Partial<{
// [K in T[keyof T]["code"]]: (...args: K[]) => TFunctionReturn<unknown>;
// }>;

0 comments on commit 10fe733

Please sign in to comment.