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

fix(v2): fix bad @docusaurus/types Plugin type generics #5058

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/docusaurus-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
"directory": "packages/docusaurus-types"
},
"license": "MIT",
"scripts": {
"test": "tsc -p ."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this supposed to catch the error? Because I tried locally and it doesn't.

Actually trying to find a solution to catch such mistakes 🤪 let me know if you find one

Copy link
Contributor Author

@jsamr jsamr Jun 25, 2021

Choose a reason for hiding this comment

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

Oh I see what happened. I tested a custom tsconfig.json file, then I realized there was a shared config and used extends. But the TS server didn't reboot and kept the former config, thus I thought the new config would still catch those errors.

And the new config brought "skipLibCheck": true which caused declaration files to be ignored. But if I revert that, all those @types/webpack error spawn.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those webpack errors come from conflicting tapable definitions. @types/webpack@4.41.29 depends on @types/tapable@^1" while webpack@5.40.0depends ontapable@^2` which provides its own definitions.

Not sure what is meant by "@types/webpack and webpack/types.d.ts are not the same thing" 😓 (in root tsconfig); but either upgrading @types/webpack to ^5 or sticking with TS types provided by webpack might fix the issue...

Those TS errors in node_modules are maddening!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Previously Webpack used definitively typed (4.x); but it started to ship official typedefs in 5.x. The initial 5.x typedefs were missing a lot of things, I think they completed support more recently so we can probably try again to remove @types/webpack once for all

},
"dependencies": {
"commander": "^5.1.0",
"joi": "^17.4.0",
"querystring": "0.2.0",
"webpack": "^5.40.0",
"webpack-merge": "^5.8.0"
},
"devDependencies": {
"typescript": "^4.3.4"
}
}
10 changes: 6 additions & 4 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export interface Props extends LoadContext, InjectedHtmlTags {
siteMetadata: DocusaurusSiteMetadata;
routes: RouteConfig[];
routesPaths: string[];
plugins: LoadedPlugin<unknown>[];
plugins: LoadedPlugin[];
}

export interface PluginContentLoadedActions {
Expand All @@ -219,7 +219,7 @@ export type AllContent = Record<
// TODO improve type (not exposed by postcss-loader)
export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};

export interface Plugin<Content> {
export interface Plugin<Content = unknown> {
name: string;
loadContent?(): Promise<Content>;
contentLoaded?({
Expand Down Expand Up @@ -283,12 +283,14 @@ export interface Plugin<Content> {
}): ThemeConfig;
}

export type InitializedPlugin = Plugin<unknown> & {
export type InitializedPlugin<Content = unknown> = Plugin<Content> & {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also adding the missing plugin content generic to these derived types

readonly options: PluginOptions;
readonly version: DocusaurusPluginVersionInformation;
};

export type LoadedPlugin = InitializedPlugin & {readonly content: unknown};
export type LoadedPlugin<Content = unknown> = InitializedPlugin<Content> & {
readonly content: Content;
};

export type PluginModule = {
<T, X>(context: LoadContext, options: T): Plugin<X>;
Expand Down
7 changes: 7 additions & 0 deletions packages/docusaurus-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18490,6 +18490,11 @@ typescript@^4.1.5:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.2.tgz#399ab18aac45802d6f2498de5054fcbbe716a805"
integrity sha512-zZ4hShnmnoVnAHpVHWpTcxdv7dWP60S2FsydQLV8V5PbS3FifjWFFRiHSWpDJahly88PRyV5teTSLoq4eG7mKw==

typescript@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.4.tgz#3f85b986945bcf31071decdd96cf8bfa65f9dcbc"
integrity sha512-uauPG7XZn9F/mo+7MrsRjyvbxFpzemRjKEZXS4AK83oP2KKOJPvb+9cO/gmnv8arWZvhnjVOXz7B49m1l0e9Ew==

ua-parser-js@^0.7.18:
version "0.7.28"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.28.tgz#8ba04e653f35ce210239c64661685bf9121dec31"
Expand Down