Skip to content

Commit 5d3684e

Browse files
committed
Document type extension pattern
Closes #643
1 parent 836feb4 commit 5d3684e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,34 @@ for await (const event of parseServerSentEvents(response)) {
11061106
}
11071107
```
11081108

1109+
### Extending types
1110+
1111+
Ky's TypeScript types are intentionally defined as type aliases rather than interfaces to prevent global module augmentation, which can lead to type conflicts and unexpected behavior across your codebase. If you need to add custom properties to Ky's types like `KyResponse` or `HTTPError`, create local wrapper types instead:
1112+
1113+
```ts
1114+
import ky, {HTTPError} from 'ky';
1115+
1116+
interface CustomError extends HTTPError {
1117+
customProperty: unknown;
1118+
}
1119+
1120+
const api = ky.extend({
1121+
hooks: {
1122+
beforeError: [
1123+
async error => {
1124+
(error as CustomError).customProperty = 'value';
1125+
return error;
1126+
}
1127+
]
1128+
}
1129+
});
1130+
1131+
// Use with type assertion
1132+
const data = (error as CustomError).customProperty;
1133+
```
1134+
1135+
This approach keeps your types scoped to where they're needed without polluting the global namespace.
1136+
11091137
## FAQ
11101138

11111139
#### How do I use this in Node.js?

0 commit comments

Comments
 (0)