From 4e75c39039385685f5c86ba8470d2f0a98f488bd Mon Sep 17 00:00:00 2001 From: Marco Pasqualetti Date: Mon, 23 Dec 2024 08:53:15 +0100 Subject: [PATCH 1/2] refactor: enable `recommendedTypeChecked` rules --- contentlayer.config.ts | 16 ++++++++-- eslint.config.mjs | 25 ++++++++++++++-- src/components/Admonition.tsx | 2 +- src/components/ApiGallery.tsx | 4 ++- src/components/ApiRefTable.tsx | 8 +++-- src/components/BuilderPage.tsx | 19 +++++++----- src/components/ClipBoard.tsx | 4 ++- src/components/CodeArea.tsx | 27 +++++++++++------ src/components/CodeCompareSection.tsx | 6 ++-- src/components/DevTools.tsx | 11 +++++-- src/components/Form.tsx | 5 ++-- src/components/FormFields.tsx | 10 +++---- src/components/Menu/Menu.tsx | 4 +-- src/components/Nav.tsx | 24 ++++++++------- src/components/Popup.tsx | 15 +++++++--- src/components/ResourceList.tsx | 12 ++++++-- src/components/Search.tsx | 15 ++++++---- src/components/TabGroup.tsx | 4 ++- src/components/Watcher.tsx | 18 +++++++---- src/components/general-observer.tsx | 8 +++-- src/components/layout.tsx | 10 ++++--- src/components/logic/generateCode.ts | 11 ++----- src/components/mdx/code.tsx | 23 ++++---------- src/components/mdx/pre.tsx | 6 ++-- src/components/mdx/theme.ts | 2 +- src/components/mdx/youtube.tsx | 43 +++++++-------------------- src/components/utils/copyClipBoard.ts | 3 +- src/components/utils/useWindowSize.ts | 4 ++- src/content/docs/useform.mdx | 3 +- src/pages/[...slug].tsx | 5 ++-- src/pages/_app.tsx | 16 +++++----- tsconfig.json | 6 +++- 32 files changed, 210 insertions(+), 159 deletions(-) diff --git a/contentlayer.config.ts b/contentlayer.config.ts index 1c3a39456..3958c85a1 100644 --- a/contentlayer.config.ts +++ b/contentlayer.config.ts @@ -4,6 +4,7 @@ import remarkGfm from "remark-gfm" import rehypeMdxCodeProps from "rehype-mdx-code-props" import emoji from "remark-emoji" import * as sidebar from "./src/components/Menu/MenuLinks" +import type { Pages } from "./src/types/types" export const Doc = defineDocumentType(() => ({ name: "Doc", @@ -33,13 +34,22 @@ export const Doc = defineDocumentType(() => ({ type: "string", resolve: (doc) => doc._raw.flattenedPath.split("/").slice(1).join("/"), }, + + /** + * Can't define value different from primitives, so hardcoding the correct type + * @see https://github.com/contentlayerdev/contentlayer/issues/149 + */ segment: { - type: "list", + type: "string[]" as "list", resolve: (doc) => doc._raw.flattenedPath.split("/"), }, pages: { - type: "list", - resolve: (doc) => sidebar[doc.sidebar] ?? [], + type: "json", + resolve: (doc) => { + // added explicit type casting to keep track of this data structure + // in case in the future contentlayer will support values different than primitives + return sidebar[doc.sidebar] as unknown as Pages + }, }, }, })) diff --git a/eslint.config.mjs b/eslint.config.mjs index d4767491e..d56f1e8fd 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -9,7 +9,8 @@ const compat = new FlatCompat({ }) export default tseslint.config( - tseslint.configs.recommended, + tseslint.configs.recommendedTypeChecked, + ...compat.config({ extends: ["next"], rules: { @@ -30,17 +31,37 @@ export default tseslint.config( "@next/next/no-img-element": "off", }, }), + // @ts-expect-error eslintPluginReact.configs.flat, but runtime is always defined eslintPluginReact.configs.flat["jsx-runtime"], + { linterOptions: { reportUnusedDisableDirectives: "error", }, + languageOptions: { + parserOptions: { + projectService: true, + tsconfigRootDir: import.meta.dirname, + }, + }, rules: { // typescript "@typescript-eslint/explicit-function-return-type": "off", - "@typescript-eslint/interface-name-prefix": "off", "@typescript-eslint/explicit-module-boundary-types": "off", + "@typescript-eslint/interface-name-prefix": "off", + "@typescript-eslint/no-floating-promises": "off", + "@typescript-eslint/restrict-template-expressions": [ + "error", + { + allowAny: false, + allowBoolean: false, + allowNever: false, + allowNullish: false, + allowNumber: true, + allowRegExp: false, + }, + ], }, } ) diff --git a/src/components/Admonition.tsx b/src/components/Admonition.tsx index b1ad3b1ae..b6921b309 100644 --- a/src/components/Admonition.tsx +++ b/src/components/Admonition.tsx @@ -109,7 +109,7 @@ export const Admonition = ({ children, }: { type: AdmonitionType - title: string + title?: string children: ReactNode }) => { return ( diff --git a/src/components/ApiGallery.tsx b/src/components/ApiGallery.tsx index d4e2623fc..24fccbb7e 100644 --- a/src/components/ApiGallery.tsx +++ b/src/components/ApiGallery.tsx @@ -12,7 +12,9 @@ export default function ApiGallery() { const router = useRouter() const onChange: MouseEventHandler = (e) => { - const version = parseInt((e.target as HTMLElement).getAttribute("value")!) + const version = parseInt( + (e.target as HTMLElement).getAttribute("value") as string + ) if (version !== 7) { router.push(`https://legacy.react-hook-form.com/v${version}/api`) diff --git a/src/components/ApiRefTable.tsx b/src/components/ApiRefTable.tsx index 076b14b0d..4fc0897f4 100644 --- a/src/components/ApiRefTable.tsx +++ b/src/components/ApiRefTable.tsx @@ -86,7 +86,9 @@ export default function ApiRefTable({ api }: { api: typeof apiData }) { {api.register.options.title}