From a9e918dc1efc01ab5cd9efce0534d417f898b6f6 Mon Sep 17 00:00:00 2001 From: James Fenn Date: Fri, 20 Oct 2023 16:51:14 -0400 Subject: [PATCH] enable tsconfig strict mode & fix type errors --- astro.config.ts | 23 +++--- build-scripts/generate-epubs.ts | 6 +- build-scripts/search-index.ts | 15 ++-- .../social-previews/layouts/banner.tsx | 7 +- .../layouts/twitter-preview.tsx | 2 +- build-scripts/social-previews/live-server.ts | 2 +- .../shared-post-preview-png.ts | 6 +- package-lock.json | 20 ++++++ package.json | 1 + .../button-radio-group/button-radio-group.tsx | 6 +- src/components/button/button.tsx | 72 +++++++++---------- .../collection-card/collection-card.tsx | 2 +- src/components/dialog/dialog.tsx | 2 +- src/components/pagination/on-click-base.ts | 2 +- .../pagination/pagination-popover.tsx | 4 +- src/components/post-card/post-card-grid.tsx | 2 +- src/components/select/select.tsx | 12 ++-- src/hooks/use-element-size.tsx | 3 +- src/pages/rss.xml.ts | 4 +- src/utils/achievements/github.ts | 6 +- src/utils/achievements/index.ts | 18 ++--- src/utils/api.ts | 5 +- src/utils/data.ts | 16 +++-- src/utils/get-largest-source-set-src.ts | 4 +- src/utils/get-suggested-articles.ts | 12 ++-- src/utils/href-container-script.ts | 14 +++- .../markdown/file-tree/rehype-file-tree.ts | 19 +++-- src/utils/markdown/hints/rehype-transform.ts | 12 ++-- src/utils/markdown/iframes/iframe-script.ts | 16 ++--- .../markdown/iframes/rehype-transform.ts | 45 ++++++------ src/utils/markdown/index.ts | 17 +++-- src/utils/markdown/rehype-astro-image-md.ts | 14 ++-- src/utils/markdown/rehype-header-class.ts | 14 ++-- src/utils/markdown/rehype-header-text.ts | 3 +- .../markdown/rehype-unicorn-element-map.ts | 10 +-- src/utils/markdown/remark-embedder-twitch.ts | 2 +- src/utils/markdown/tabs/rehype-transform.ts | 13 ++-- src/utils/markdown/tabs/tabs-script.ts | 44 ++++++------ .../markdown/tooltips/rehype-transform.ts | 14 ++-- .../twoslash-tabindex/rehype-transform.ts | 2 +- src/utils/translations.ts | 16 ++--- src/views/about/components.tsx | 2 +- .../dark-light-button/theme-toggle.ts | 6 +- src/views/blog-post/series/base.ts | 2 +- .../cover-layers/shared/repeat-background.tsx | 3 + .../scripts/color-change-listener.ts | 3 + .../scripts/parallax-scrolling.ts | 3 + src/views/search/components/filter-dialog.tsx | 6 +- .../search/components/filter-display.tsx | 8 +-- .../search/components/filter-section.tsx | 2 +- .../search/components/filter-sidebar.tsx | 2 +- src/views/search/components/search-hero.tsx | 4 +- .../search/components/search-result-count.tsx | 2 +- src/views/search/components/search-topbar.tsx | 2 +- src/views/search/search-page.spec.tsx | 10 +-- src/views/search/search-page.tsx | 6 +- tsconfig.json | 1 + 57 files changed, 323 insertions(+), 246 deletions(-) diff --git a/astro.config.ts b/astro.config.ts index 7e37fff4..251907ff 100644 --- a/astro.config.ts +++ b/astro.config.ts @@ -36,19 +36,24 @@ export default defineConfig({ lastmod: new Date(), i18n: { defaultLocale: "en", - locales: Object.keys(languages).reduce((prev, key) => { - prev[key] = fileToOpenGraphConverter(key as keyof typeof languages); - return prev; - }, {}), + locales: Object.keys(languages).reduce( + (prev, key) => { + prev[key] = fileToOpenGraphConverter(key as keyof typeof languages); + return prev; + }, + {} as Record, + ), }, filter(page) { // return true, unless lart part of the URL ends with "_noindex" // in which case it should not be in the sitemap - return !page - .split("/") - .filter((part) => !!part.length) - .at(-1) - .endsWith("_noindex"); + return !( + page + .split("/") + .filter((part) => !!part.length) + .at(-1) + ?.endsWith("_noindex") ?? false + ); }, serialize({ url, ...rest }) { return { diff --git a/build-scripts/generate-epubs.ts b/build-scripts/generate-epubs.ts index 9e433001..deb97ea2 100644 --- a/build-scripts/generate-epubs.ts +++ b/build-scripts/generate-epubs.ts @@ -58,9 +58,9 @@ async function generateCollectionEPub( collectionPosts: PostInfo[], fileLocation: string, ) { - const authors = collection.authors.map((id) => { - return getUnicornById(id, collection.locale).name; - }); + const authors = collection.authors + .map((id) => getUnicornById(id, collection.locale)?.name) + .filter((name): name is string => !!name); const epub = new EPub( { diff --git a/build-scripts/search-index.ts b/build-scripts/search-index.ts index 27371eaa..9ae483ae 100644 --- a/build-scripts/search-index.ts +++ b/build-scripts/search-index.ts @@ -32,7 +32,7 @@ const createPostIndex = () => { getFn: (post) => { return post.authors .map((id) => api.getUnicornById(id, post.locale)) - .flatMap((author) => Object.values(author.socials)) + .flatMap((author) => Object.values(author!.socials)) .join(", "); }, weight: 1.2, @@ -60,7 +60,7 @@ const createCollectionIndex = () => { name: "authorName", getFn: (post) => { return post.authors - .map((id) => api.getUnicornById(id, post.locale).name) + .map((id) => api.getUnicornById(id, post.locale)!.name) .join(", "); }, weight: 1.8, @@ -70,7 +70,7 @@ const createCollectionIndex = () => { getFn: (post) => { return post.authors .map((id) => api.getUnicornById(id, post.locale)) - .flatMap((author) => Object.values(author.socials)) + .flatMap((author) => Object.values(author!.socials)) .join(", "); }, weight: 1.2, @@ -87,12 +87,13 @@ const createCollectionIndex = () => { const postIndex = createPostIndex(); const collectionIndex = createCollectionIndex(); -const unicorns: Record = api - .getUnicornsByLang("en") - .reduce((obj, unicorn) => { +const unicorns = api.getUnicornsByLang("en").reduce( + (obj, unicorn) => { obj[unicorn.id] = unicorn; return obj; - }, {}); + }, + {} as Record, +); const json = JSON.stringify({ postIndex, diff --git a/build-scripts/social-previews/layouts/banner.tsx b/build-scripts/social-previews/layouts/banner.tsx index 543252b4..e5ed0525 100644 --- a/build-scripts/social-previews/layouts/banner.tsx +++ b/build-scripts/social-previews/layouts/banner.tsx @@ -4,8 +4,10 @@ import style from "./banner-css"; import classnames from "classnames"; import tags from "../../../content/data/tags.json"; import fs from "fs"; +import { TagInfo } from "types/TagInfo"; const TAG_SVG_DEFAULT = fs.readFileSync("public/stickers/role_devops.svg", "utf-8"); +const tagsMap = new Map(Object.entries(tags)); function BannerCodeScreen({ post, @@ -19,8 +21,9 @@ function BannerCodeScreen({ const rotX = (post.description.length % 20) - 10; const rotY = (post.title.length * 3) % 20; - const tagInfo = post.tags.map(tag => tags[tag]) - .filter(t => t?.emoji || (t?.image && t?.shownWithBranding))[0]; + const tagInfo = post.tags.map(tag => tagsMap.get(tag)) + .filter((t): t is TagInfo => !!t) + .filter(t => t.emoji || (t.image && t.shownWithBranding))[0]; const tagSvg = tagInfo?.image ? fs.readFileSync("public" + tagInfo.image, "utf-8") diff --git a/build-scripts/social-previews/layouts/twitter-preview.tsx b/build-scripts/social-previews/layouts/twitter-preview.tsx index 18070eca..c301f1db 100644 --- a/build-scripts/social-previews/layouts/twitter-preview.tsx +++ b/build-scripts/social-previews/layouts/twitter-preview.tsx @@ -72,7 +72,7 @@ const TwitterLargeCard = ({