Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#61790 feat(node): Error.cause property sup…
Browse files Browse the repository at this point in the history
  • Loading branch information
peterblazejewicz authored Aug 19, 2022
1 parent d41e35f commit 8f7c220
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
23 changes: 23 additions & 0 deletions types/node/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
// Error extensions
interface Error {
/**
* If present, the `error.cause` property is the underlying cause of the `Error`.
* It is used when catching an error and throwing a new one with a different message or code in order to still have access to the original error.
* @added v16.9.0
*/
cause?: unknown;
}

/**
* @added v16.9.0
*/
interface ErrorOptions {
/**
* The error that caused the newly created error.
* @added v16.9.0
*/
cause?: unknown;
}

// Declare "static" methods in Error
interface ErrorConstructor {
new(message?: string, options?: ErrorOptions): Error;
(message?: string, options?: ErrorOptions): Error;
/** Create .stack property on a target object */
captureStackTrace(targetObject: object, constructorOpt?: Function): void;

Expand Down
7 changes: 7 additions & 0 deletions types/node/test/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ declare var RANDOM_GLOBAL_VARIABLE: true;
const arrayBuffer = new ArrayBuffer(0);
structuredClone({ test: arrayBuffer }, { transfer: [arrayBuffer] }); // $ExpectType { test: ArrayBuffer; }
}

{
// Error extensions
const cause = new Error('The remote HTTP server responded with a 500 status');
const symptom = new Error('The message failed to send', { cause });
symptom.cause; // $ExpectType unknown
}
23 changes: 23 additions & 0 deletions types/node/v16/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
// Error extensions
interface Error {
/**
* If present, the `error.cause` property is the underlying cause of the `Error`.
* It is used when catching an error and throwing a new one with a different message or code in order to still have access to the original error.
* @added v16.9.0
*/
cause?: unknown;
}

/**
* @added v16.9.0
*/
interface ErrorOptions {
/**
* The error that caused the newly created error.
* @added v16.9.0
*/
cause?: unknown;
}

// Declare "static" methods in Error
interface ErrorConstructor {
new(message?: string, options?: ErrorOptions): Error;
(message?: string, options?: ErrorOptions): Error;
/** Create .stack property on a target object */
captureStackTrace(targetObject: object, constructorOpt?: Function): void;

Expand Down
7 changes: 7 additions & 0 deletions types/node/v16/test/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ declare var RANDOM_GLOBAL_VARIABLE: true;
gc();
}
}

{
// Error extensions
const cause = new Error('The remote HTTP server responded with a 500 status');
const symptom = new Error('The message failed to send', { cause });
symptom.cause; // $ExpectType unknown
}

0 comments on commit 8f7c220

Please sign in to comment.