diff --git a/types/node/globals.d.ts b/types/node/globals.d.ts index da499940ebbaa4..84d8f9166dd257 100644 --- a/types/node/globals.d.ts +++ b/types/node/globals.d.ts @@ -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; diff --git a/types/node/test/globals.ts b/types/node/test/globals.ts index ba63f2f81a8d6b..b9d584ec86c040 100644 --- a/types/node/test/globals.ts +++ b/types/node/test/globals.ts @@ -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 +} diff --git a/types/node/v16/globals.d.ts b/types/node/v16/globals.d.ts index 3d230a5edd1350..a740ac2826306a 100644 --- a/types/node/v16/globals.d.ts +++ b/types/node/v16/globals.d.ts @@ -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; diff --git a/types/node/v16/test/globals.ts b/types/node/v16/test/globals.ts index ab86bd8a66cbf8..86a861ae224b6d 100644 --- a/types/node/v16/test/globals.ts +++ b/types/node/v16/test/globals.ts @@ -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 +}