Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fixed type issues in _createLoaderContext #19233

Merged

Conversation

Andarist
Copy link
Contributor

This PR makes the codebase compatible with the (probably) incoming microsoft/TypeScript#56907 . At the moment, contextually-typed functions in JS files are overly permissive - unlike their TS counterparts. See the comparison here

What kind of change does this PR introduce?

internal types fix

Did you add tests for your changes?

No

Does this PR introduce a breaking change?

No

What needs to be documented once your changes are merged?

Nothing

@@ -648,6 +648,10 @@ class NormalModule extends Module {
/** @type {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} */
const loaderContext = {
version: 2,
/**
* @param {import("../declarations/LoaderContext").Schema} [schema]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main purpose of this annotation is to make schema parameter optional, so this function can be correctly matched against both getOptions overloads

child.resolve({}, context, request, getResolveContext(), callback);
} else {
return new Promise((resolve, reject) => {
return /** @type {ReturnType<import("../declarations/LoaderContext").NormalModuleLoaderContext<T>["getResolve"]>} */ (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces a cast, which I think is fine here. You can only type overloads using overloads signatures + 1 extra implementations signature. Since you want to benefit here from contextual typing... this function here is your implementation signature already and you can bring in the public overloads with a cast. It's either this or retyping everything locally here with regular~ overloads syntax or just changing the types to accommodate the upcoming change in some other way.

@Andarist Andarist force-pushed the fix/contextual-signature-compat branch from bac1591 to 907cd7e Compare February 15, 2025 08:47
@TheLarkInn TheLarkInn requested a review from Copilot February 18, 2025 19:45

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

@alexander-akait alexander-akait merged commit 3ee8d5f into webpack:main Feb 27, 2025
62 of 63 checks passed
@alexander-akait
Copy link
Member

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants