Bug Description
The type definition for defining Client interceptors doesn't match the documentation. The Client constructor is expecting an array of Dispatchers, not the interceptor functions.
Reproducible By
Install TypeScript 5.2.2, undici 5.24.0.
import { Client, Dispatcher } from "undici";
const insertHeaderInterceptor = dispatch => {
return function InterceptedDispatch(opts, handler){
opts.headers.push('Authorization', 'Bearer [Some token]')
return dispatch(opts, handler)
}
}
const client = new Client('https://localhost:3000', {
interceptors: { Client: [insertHeaderInterceptor] } // error here
})
The error returned by TypeScript:
src/lib/quote/try.ts:13:28 - error TS2322: Type '(dispatch: (options: DispatchOptions, handler: DispatchHandlers) => boolean) => (opts: DispatchOptions, handler: DispatchHandlers) => boolean' is not assignable to type 'Dispatcher'.
13 interceptors: { Client: [insertHeaderInterceptor] },
~~~~~~~~~~~~~~~~~~~~~~~
I also tried:
import { Client, Dispatcher } from "undici";
// the change from above is explicitly defining the type for the interceptor to match
// the intent
const insertHeaderInterceptor: Dispatcher.DispatchInterceptor = (
dispatch: Dispatcher["dispatch"]
) => {
return function InterceptedDispatch(
opts: Dispatcher.DispatchOptions,
handler: Dispatcher.DispatchHandlers
) {
return dispatch(opts, handler);
};
};
const client = new Client("https://localhost:3000", {
interceptors: { Client: [insertHeaderInterceptor] },
});
But the error is the same:
src/lib/quote/try.ts:15:28 - error TS2322: Type 'DispatchInterceptor' is not assignable to type 'Dispatcher'.
15 interceptors: { Client: [insertHeaderInterceptor] },
~~~~~~~~~~~~~~~~~~~~~~~
Expected Behavior
The code sample should successfully type check.
Environment
Windows 11 / WSL - Ubuntu 20.04 / Node v20.6.1 / TypeScript 5.2.2
Additional context
It looks like this bug comes from using the default import multiple times in types/client.d.ts - that imports the Dispatcher class twice, not the DispatchInterceptor interface.
Bug Description
The type definition for defining Client interceptors doesn't match the documentation. The Client constructor is expecting an array of Dispatchers, not the interceptor functions.
Reproducible By
Install TypeScript 5.2.2, undici 5.24.0.
The error returned by TypeScript:
I also tried:
But the error is the same:
Expected Behavior
The code sample should successfully type check.
Environment
Windows 11 / WSL - Ubuntu 20.04 / Node v20.6.1 / TypeScript 5.2.2
Additional context
It looks like this bug comes from using the default import multiple times in
types/client.d.ts- that imports the Dispatcher class twice, not theDispatchInterceptorinterface.