rspack
rspack copied to clipboard
[Bug]: Missing typings for `externals` function (zod types not inferred correctly)
System Info
@rspack/core version: 1.0.7
TypeScript version: 5.5.4
(other system info is irrelevant because this is a typescript problem)
Details
I was trying to use an externals function in a configuration, and TS can't infer the proper argument types or return type based on the generated zod types for Externals.
I ended up manually writing the type (ctx: ExternalItemFunctionData) => Promise<ExternalItemValue | undefined> based on looking at the source code. But it would be better if the typings would work properly without any extra work.
(The undefined return is also missing from the zod types, and that seems to be the way to indicate that an item shouldn't be externalized?)
Reproduce link
No response
Reproduce Steps
Make a TS file with the following contents and observe the errors.
import type { Configuration, ExternalItem } from '@rspack/core';
const config: Configuration = {
externals:
// `ctx` type and return type are any
async (ctx) => {
// `ctx` type is any, so `ctx.request` type is missing
if (ctx.request?.includes('foo')) {
return ctx.request;
}
},
};
type ExternalsFunction1 = Extract<ExternalItem, (ctx: Object) => Promise<unknown>>;
// `ctx` has a type, but there's an error that undefined is not a valid return value
const externals1: ExternalsFunction1 = async (ctx) => {
if (ctx.request?.includes('foo')) {
return ctx.request;
}
return;
};
// I couldn't get the types for the callback version to be inferred at all.
// `ctx` arg type is correct, other args get typed as `...arg1: unknown[]`
type ExternalsFunction2 = Extract<ExternalItem, (ctx: Object, callback: Function) => void>;
// also there's the error again that undefined is not a valid return value
const externals2: ExternalsFunction2 = (ctx, callback) => {
if (ctx.request?.includes('foo')) {
// unknown type
callback(ctx.request);
}
callback();
};