Skip to content

Commit

Permalink
ESLint fixes (renamed all generic params)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMessinger committed Jul 18, 2020
1 parent b8db52e commit a1fa89a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export { constructor as Ono };
function Ono<T extends ErrorLike>(ErrorConstructor: ErrorLikeConstructor<T>, options?: OnoOptions) {
options = normalizeOptions(options);

function ono<E extends ErrorLike, P extends object>(...args: unknown[]) {
let { originalError, props, message } = normalizeArgs<E, P>(args, options!);
function ono<TError extends ErrorLike, TProps extends object>(...args: unknown[]) {
let { originalError, props, message } = normalizeArgs<TError, TProps>(args, options!);

// Create a new error of the specified type
let newError = new (ErrorConstructor as ErrorLikeConstructorClass<T>)(message);
Expand Down
6 changes: 4 additions & 2 deletions src/extend-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const protectedProps: Array<string | symbol> = ["name", "message", "stack"];
* @param originalError - The original error object, if any
* @param props - Additional properties to add, if any
*/
export function extendError<T extends ErrorLike, E extends ErrorLike, P extends object>(error: T, originalError?: E, props?: P) {
let onoError = error as unknown as T & E & P & OnoError<T & E & P>;
export function extendError<TError extends ErrorLike, TOriginal extends ErrorLike, TProps extends object>(
error: TError, originalError?: TOriginal, props?: TProps) {

let onoError = error as unknown as TError & TOriginal & TProps & OnoError<TError & TOriginal & TProps>;

extendStack(onoError, originalError);

Expand Down
14 changes: 7 additions & 7 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export function normalizeOptions(options?: OnoOptions): OnoOptions {
/**
* Normalizes the Ono arguments, accounting for defaults, options, and optional arguments.
*/
export function normalizeArgs<E extends ErrorLike, P extends object>(args: unknown[], options: OnoOptions) {
let originalError: E | undefined;
let props: P | undefined;
export function normalizeArgs<TError extends ErrorLike, TProps extends object>(args: unknown[], options: OnoOptions) {
let originalError: TError | undefined;
let props: TProps | undefined;
let formatArgs: unknown[];
let message = "";

Expand All @@ -28,16 +28,16 @@ export function normalizeArgs<E extends ErrorLike, P extends object>(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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const onoMap = ono as unknown as Record<string, Ono<Error>>;
* 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<E extends ErrorLike, P extends object>(...args: unknown[]) {
function ono<TError extends ErrorLike, TProps extends object>(...args: unknown[]) {
let originalError = args[0] as ErrorLike | undefined;

// Is the first argument an Error-like object?
Expand Down
4 changes: 2 additions & 2 deletions src/to-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<E extends ErrorLike>(this: E): ErrorPOJO & E {
export function toJSON<T extends ErrorLike>(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 = {};
Expand All @@ -25,7 +25,7 @@ export function toJSON<E extends ErrorLike>(this: E): ErrorPOJO & E {
}
}

return pojo as ErrorPOJO & E;
return pojo as ErrorPOJO & T;
}


Expand Down
20 changes: 10 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<E extends ErrorLike>(error: E): ErrorPOJO & E;
toJSON<T extends ErrorLike>(error: T): T & ErrorPOJO;

/**
* Extends the given Error object with enhanced Ono functionality, such as improved support for
Expand All @@ -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<T extends ErrorLike, P extends object>(error: T, props?: P): T & P & OnoError<T & P>;
extend<TError extends ErrorLike, TProps extends object>(error: TError, props?: TProps): TError & TProps & OnoError<TError & TProps>;

/**
* Extends the given Error object with enhanced Ono functionality, such as nested stack traces
Expand All @@ -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<T extends ErrorLike, E extends ErrorLike>(error: T, originalError?: E): T & E & OnoError<T & E>;
extend<TError extends ErrorLike, TOriginal extends ErrorLike>(error: TError, originalError?: TOriginal): TError & TOriginal & OnoError<TError & TOriginal>;

/**
* Extends the given Error object with enhanced Ono functionality, such as nested stack traces,
Expand All @@ -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<T extends ErrorLike, E extends ErrorLike, P extends object>(error: T, originalError?: E, props?: P): T & E & P & OnoError<T & E & P>;
extend<TError extends ErrorLike, TOriginal extends ErrorLike, TProps extends object>(error: TError, originalError?: TOriginal, props?: TProps): TError & TOriginal & TProps & OnoError<TError & TOriginal & TProps>;
}

/**
Expand All @@ -77,7 +77,7 @@ export interface Ono<T extends ErrorLike> {
*
* @param error - The original error
*/
<E extends ErrorLike>(error: E): T & E & OnoError<T & E>;
<TError extends ErrorLike>(error: TError): T & TError & OnoError<T & TError>;

/**
* Creates a new error with the message, stack trace, and properties of another error,
Expand All @@ -86,7 +86,7 @@ export interface Ono<T extends ErrorLike> {
* @param error - The original error
* @param props - An object whose properties will be added to the returned error
*/
<E extends ErrorLike, P extends object>(error: E, props: P): T & E & P & OnoError<T & E & P>;
<TError extends ErrorLike, TProps extends object>(error: TError, props: TProps): T & TError & TProps & OnoError<T & TError & TProps>;

/**
* Creates a new error with a formatted message and the stack trace and properties of another error.
Expand All @@ -95,7 +95,7 @@ export interface Ono<T extends ErrorLike> {
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
<E extends ErrorLike>(error: E, message: string, ...params: unknown[]): T & E & OnoError<T & E>;
<TError extends ErrorLike>(error: TError, message: string, ...params: unknown[]): T & TError & OnoError<T & TError>;

/**
* Creates a new error with a formatted message and the stack trace and properties of another error,
Expand All @@ -106,7 +106,7 @@ export interface Ono<T extends ErrorLike> {
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
<E extends ErrorLike, P extends object>(error: E, props: P, message: string, ...params: unknown[]): T & E & P & OnoError<T & E & P>;
<TError extends ErrorLike, TProps extends object>(error: TError, props: TProps, message: string, ...params: unknown[]): T & TError & TProps & OnoError<T & TError & TProps>;

/**
* Creates an error with a formatted message.
Expand All @@ -121,7 +121,7 @@ export interface Ono<T extends ErrorLike> {
*
* @param props - An object whose properties will be added to the returned error
*/
<P extends object>(props: P): T & P & OnoError<T & P>;
<TProps extends object>(props: TProps): T & TProps & OnoError<T & TProps>;

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

/**
Expand Down

0 comments on commit a1fa89a

Please sign in to comment.