Skip to content
Myr

Document type extension pattern

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #643

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 836feb4 · Browse files · 1 file+28 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+28 added−0 removed

  • other:+28 added−0 removed

1 file+28 added−0 removed

modified:readme.md+28 added
1104 unmodified lines
1105 console.log(event.data);1106}1107```11081109### Extending types11101111Ky'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:11121113```ts1114import ky, {HTTPError} from 'ky';11151116interface CustomError extends HTTPError {1117 customProperty: unknown;1118}11191120const api = ky.extend({1121 hooks: {1122 beforeError: [1123 async error => {1124 (error as CustomError).customProperty = 'value';1125 return error;1126 }1127 ]1128 }1129});11301131// Use with type assertion1132const data = (error as CustomError).customProperty;1133```11341135This approach keeps your types scoped to where they're needed without polluting the global namespace.11361137## FAQ11381139#### How do I use this in Node.js?1140
61 unmodified lines