diff --git a/src/constructor.ts b/src/constructor.ts index a0bbbfc..7507b8f 100644 --- a/src/constructor.ts +++ b/src/constructor.ts @@ -13,8 +13,8 @@ export { constructor as Ono }; function Ono(ErrorConstructor: ErrorLikeConstructor, options?: OnoOptions) { options = normalizeOptions(options); - function ono(...args: unknown[]) { - let { originalError, props, message } = normalizeArgs(args, options!); + function ono(...args: unknown[]) { + let { originalError, props, message } = normalizeArgs(args, options!); // Create a new error of the specified type let newError = new (ErrorConstructor as ErrorLikeConstructorClass)(message); diff --git a/src/extend-error.ts b/src/extend-error.ts index 52e0c9b..c80e053 100644 --- a/src/extend-error.ts +++ b/src/extend-error.ts @@ -12,8 +12,10 @@ const protectedProps: Array = ["name", "message", "stack"]; * @param originalError - The original error object, if any * @param props - Additional properties to add, if any */ -export function extendError(error: T, originalError?: E, props?: P) { - let onoError = error as unknown as T & E & P & OnoError; +export function extendError( + error: TError, originalError?: TOriginal, props?: TProps) { + + let onoError = error as unknown as TError & TOriginal & TProps & OnoError; extendStack(onoError, originalError); diff --git a/src/normalize.ts b/src/normalize.ts index 2ef85fc..425e2f5 100644 --- a/src/normalize.ts +++ b/src/normalize.ts @@ -16,9 +16,9 @@ export function normalizeOptions(options?: OnoOptions): OnoOptions { /** * Normalizes the Ono arguments, accounting for defaults, options, and optional arguments. */ -export function normalizeArgs(args: unknown[], options: OnoOptions) { - let originalError: E | undefined; - let props: P | undefined; +export function normalizeArgs(args: unknown[], options: OnoOptions) { + let originalError: TError | undefined; + let props: TProps | undefined; let formatArgs: unknown[]; let message = ""; @@ -28,16 +28,16 @@ export function normalizeArgs(args: unkno } else if (typeof args[1] === "string") { if (args[0] instanceof Error) { - originalError = args[0] as E; + originalError = args[0] as TError; } else { - props = args[0] as P; + props = args[0] as TProps; } formatArgs = args.slice(1); } else { - originalError = args[0] as E; - props = args[1] as P; + originalError = args[0] as TError; + props = args[1] as TProps; formatArgs = args.slice(2); } diff --git a/src/singleton.ts b/src/singleton.ts index b088dca..db72f19 100644 --- a/src/singleton.ts +++ b/src/singleton.ts @@ -19,7 +19,7 @@ const onoMap = ono as unknown as Record>; * Creates a new error with the specified message, properties, and/or inner error. * If an inner error is provided, then the new error will match its type, if possible. */ -function ono(...args: unknown[]) { +function ono(...args: unknown[]) { let originalError = args[0] as ErrorLike | undefined; // Is the first argument an Error-like object? diff --git a/src/to-json.ts b/src/to-json.ts index e18db8c..09bab6d 100644 --- a/src/to-json.ts +++ b/src/to-json.ts @@ -8,7 +8,7 @@ const objectPrototype = Object.getPrototypeOf({}); * Custom JSON serializer for Error objects. * Returns all built-in error properties, as well as extended properties. */ -export function toJSON(this: E): ErrorPOJO & E { +export function toJSON(this: T): ErrorPOJO & T { // HACK: We have to cast the objects to `any` so we can use symbol indexers. // see https://github.com/Microsoft/TypeScript/issues/1863 let pojo: any = {}; @@ -25,7 +25,7 @@ export function toJSON(this: E): ErrorPOJO & E { } } - return pojo as ErrorPOJO & E; + return pojo as ErrorPOJO & T; } diff --git a/src/types.ts b/src/types.ts index 552c74a..1b3a74b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,7 +24,7 @@ export interface OnoConstructor { * Returns an object containing all properties of the given Error object, * which can be used with `JSON.stringify()`. */ - toJSON(error: E): ErrorPOJO & E; + toJSON(error: T): T & ErrorPOJO; /** * Extends the given Error object with enhanced Ono functionality, such as improved support for @@ -41,7 +41,7 @@ export interface OnoConstructor { * @param error - The error object to extend. This object instance will be modified and returned. * @param props - An object whose properties will be added to the error */ - extend(error: T, props?: P): T & P & OnoError; + extend(error: TError, props?: TProps): TError & TProps & OnoError; /** * Extends the given Error object with enhanced Ono functionality, such as nested stack traces @@ -50,7 +50,7 @@ export interface OnoConstructor { * @param error - The error object to extend. This object instance will be modified and returned. * @param originalError - The original error. This error's stack trace will be added to the error's stack trace. */ - extend(error: T, originalError?: E): T & E & OnoError; + extend(error: TError, originalError?: TOriginal): TError & TOriginal & OnoError; /** * Extends the given Error object with enhanced Ono functionality, such as nested stack traces, @@ -60,7 +60,7 @@ export interface OnoConstructor { * @param originalError - The original error. This error's stack trace will be added to the error's stack trace. * @param props - An object whose properties will be added to the error */ - extend(error: T, originalError?: E, props?: P): T & E & P & OnoError; + extend(error: TError, originalError?: TOriginal, props?: TProps): TError & TOriginal & TProps & OnoError; } /** @@ -77,7 +77,7 @@ export interface Ono { * * @param error - The original error */ - (error: E): T & E & OnoError; + (error: TError): T & TError & OnoError; /** * Creates a new error with the message, stack trace, and properties of another error, @@ -86,7 +86,7 @@ export interface Ono { * @param error - The original error * @param props - An object whose properties will be added to the returned error */ - (error: E, props: P): T & E & P & OnoError; + (error: TError, props: TProps): T & TError & TProps & OnoError; /** * Creates a new error with a formatted message and the stack trace and properties of another error. @@ -95,7 +95,7 @@ export interface Ono { * @param message - The new error message, possibly including argument placeholders * @param params - Optional arguments to replace the corresponding placeholders in the message */ - (error: E, message: string, ...params: unknown[]): T & E & OnoError; + (error: TError, message: string, ...params: unknown[]): T & TError & OnoError; /** * Creates a new error with a formatted message and the stack trace and properties of another error, @@ -106,7 +106,7 @@ export interface Ono { * @param message - The new error message, possibly including argument placeholders * @param params - Optional arguments to replace the corresponding placeholders in the message */ - (error: E, props: P, message: string, ...params: unknown[]): T & E & P & OnoError; + (error: TError, props: TProps, message: string, ...params: unknown[]): T & TError & TProps & OnoError; /** * Creates an error with a formatted message. @@ -121,7 +121,7 @@ export interface Ono { * * @param props - An object whose properties will be added to the returned error */ -

(props: P): T & P & OnoError; + (props: TProps): T & TProps & OnoError; /** * Creates an error with a formatted message and additional properties. @@ -130,7 +130,7 @@ export interface Ono { * @param message - The new error message, possibly including argument placeholders * @param params - Optional arguments to replace the corresponding placeholders in the message */ -

(props: P, message: string, ...params: unknown[]): T & P & OnoError; + (props: TProps, message: string, ...params: unknown[]): T & TProps & OnoError; } /**