Skip to content

Commit d8233d9

Browse files
fix(types): restore AxiosError.cause type from unknown to Error (#7327)
* Initial plan * fix(types): restore AxiosError.cause type from unknown to Error Co-authored-by: jasonsaayman <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jasonsaayman <[email protected]>
1 parent 5945e40 commit d8233d9

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

index.d.cts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ declare class AxiosError<T = unknown, D = any> extends Error {
100100
isAxiosError: boolean;
101101
status?: number;
102102
toJSON: () => object;
103-
cause?: unknown;
103+
cause?: Error;
104104
event?: BrowserProgressEvent;
105105
static from<T = unknown, D = any>(
106106
error: Error | unknown,

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ export class AxiosError<T = unknown, D = any> extends Error {
431431
isAxiosError: boolean;
432432
status?: number;
433433
toJSON: () => object;
434-
cause?: unknown;
434+
cause?: Error;
435435
event?: BrowserProgressEvent;
436436
static from<T = unknown, D = any>(
437437
error: Error | unknown,

test/module/typings/esm/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,3 +720,12 @@ axios.get('/user', {
720720
return ['127.0.0.1', 4];
721721
}
722722
});
723+
724+
// AxiosError.cause should be typed as Error to allow accessing .message
725+
axios.get('/user').catch((error: AxiosError) => {
726+
if (error.cause) {
727+
// This should not produce a type error - cause is typed as Error
728+
const causeMessage: string | undefined = error.cause.message;
729+
console.log(causeMessage);
730+
}
731+
});

0 commit comments

Comments
 (0)