diff --git a/docs/recipes/typescript.md b/docs/recipes/typescript.md index 7966caa8f..01faa363c 100644 --- a/docs/recipes/typescript.md +++ b/docs/recipes/typescript.md @@ -151,6 +151,20 @@ test('an actual test', t => { }); ``` +Alternatively, you can extend the type globally: + +```ts +declare module 'ava' { + interface AvaContext { + foo: string; + } +} + +test.beforeEach(t => { + t.context = {foo: 'bar'}; +}); +``` + Note that, despite the type cast above, when executing `t.context` is an empty object unless it's assigned. ## Typing `throws` assertions diff --git a/types/test-fn.d.cts b/types/test-fn.d.cts index 77c6be31d..333906430 100644 --- a/types/test-fn.d.cts +++ b/types/test-fn.d.cts @@ -72,7 +72,9 @@ export type Macro = { /** A test or hook implementation. */ export type Implementation = ImplementationFn | Macro; -export type TestFn = { +interface AvaContext {} + +export type TestFn = { /** Declare a concurrent test. Additional arguments are passed to the implementation or macro. */ (title: string, implementation: Implementation, ...args: Args): void;