Skip to content

Commit 788acd9

Browse files
committed
Fix astro check errors on strict typescript, issue #293
1 parent cf40b9d commit 788acd9

File tree

7 files changed

+42
-26
lines changed

7 files changed

+42
-26
lines changed

src/components/blog/Pagination.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { getPermalink } from '~/utils/permalinks';
44
import Button from '~/components/ui/Button.astro';
55
66
export interface Props {
7-
prevUrl: string;
8-
nextUrl: string;
7+
prevUrl?: string;
8+
nextUrl?: string;
99
prevText?: string;
1010
nextText?: string;
1111
}

src/pages/[...blog]/[...page].astro

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
3+
24
import Layout from '~/layouts/PageLayout.astro';
35
import BlogList from '~/components/blog/List.astro';
46
import Headline from '~/components/blog/Headline.astro';
@@ -9,11 +11,13 @@ import { blogListRobots, getStaticPathsBlogList } from '~/utils/blog';
911
1012
export const prerender = true;
1113
12-
export async function getStaticPaths ({ paginate }) {
14+
export const getStaticPaths = (async ({ paginate }) => {
1315
return await getStaticPathsBlogList({ paginate });
14-
}
16+
}) satisfies GetStaticPaths;
17+
18+
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
1519
16-
const { page } = Astro.props;
20+
const { page } = Astro.props as Props;
1721
const currentPage = page.currentPage ?? 1;
1822
1923
// const allCategories = await findCategories();

src/pages/[...blog]/[category]/[...page].astro

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
23
import { blogCategoryRobots, getStaticPathsBlogCategory } from '~/utils/blog';
34
45
import Layout from '~/layouts/PageLayout.astro';
@@ -8,11 +9,13 @@ import Pagination from '~/components/blog/Pagination.astro';
89
910
export const prerender = true;
1011
11-
export async function getStaticPaths ({ paginate }) {
12+
export const getStaticPaths = (async ({ paginate }) => {
1213
return await getStaticPathsBlogCategory({ paginate });
13-
}
14+
}) satisfies GetStaticPaths;
1415
15-
const { page, category } = Astro.props;
16+
type Props = InferGetStaticPropsType<typeof getStaticPaths> & { category: string };
17+
18+
const { page, category } = Astro.props as Props;
1619
1720
const currentPage = page.currentPage ?? 1;
1821

src/pages/[...blog]/[tag]/[...page].astro

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
23
import { blogTagRobots, getStaticPathsBlogTag } from '~/utils/blog';
34
45
import Layout from '~/layouts/PageLayout.astro';
@@ -8,11 +9,13 @@ import Pagination from '~/components/blog/Pagination.astro';
89
910
export const prerender = true;
1011
11-
export async function getStaticPaths ({ paginate }) {
12+
export const getStaticPaths = (async ({ paginate }) => {
1213
return await getStaticPathsBlogTag({ paginate });
13-
}
14+
}) satisfies GetStaticPaths;
1415
15-
const { page, tag } = Astro.props;
16+
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
17+
18+
const { page, tag } = Astro.props as Props;
1619
1720
const currentPage = page.currentPage ?? 1;
1821

src/pages/[...blog]/index.astro

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
3+
24
import merge from 'lodash.merge';
35
import type { ImageMetadata } from 'astro';
46
import Layout from '~/layouts/PageLayout.astro';
@@ -8,17 +10,20 @@ import ToBlogLink from '~/components/blog/ToBlogLink.astro';
810
import { getCanonical, getPermalink } from '~/utils/permalinks';
911
import { getStaticPathsBlogPost, blogPostRobots } from '~/utils/blog';
1012
import { findImage } from '~/utils/images';
13+
import type { MetaData } from '~/types';
1114
1215
export const prerender = true;
1316
14-
export async function getStaticPaths () {
17+
export const getStaticPaths = (async () => {
1518
return await getStaticPathsBlogPost();
16-
}
19+
}) satisfies GetStaticPaths;
20+
21+
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
1722
18-
const { post } = Astro.props;
23+
const { post } = Astro.props as Props;
1924
2025
const url = getCanonical(getPermalink(post.permalink, 'post'));
21-
const image = (await findImage(post.image)) as ImageMetadata | undefined;
26+
const image = (await findImage(post.image)) as ImageMetadata | string | undefined;
2227
2328
const metadata = merge(
2429
{
@@ -30,11 +35,11 @@ const metadata = merge(
3035
},
3136
openGraph: {
3237
type: 'article',
33-
...(image ? { images: [{ url: image, width: image?.width, height: image?.height }] } : {}),
38+
...(image ? { images: [{ url: image, width: (image as ImageMetadata)?.width, height: (image as ImageMetadata)?.height }] } : {}),
3439
},
3540
},
3641
{ ...(post?.metadata ? { ...post.metadata, canonical: post.metadata?.canonical || url } : {}) }
37-
);
42+
) as MetaData;
3843
---
3944

4045
<Layout metadata={metadata}>

src/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AstroComponentFactory } from 'astro/runtime/server/index.js';
2-
import type { HTMLAttributes } from 'astro/types';
2+
import type { HTMLAttributes, ImageMetadata } from 'astro/types';
33

44
export interface Post {
55
/** A unique ID number that identifies a post. */
@@ -21,7 +21,7 @@ export interface Post {
2121
/** Optional summary of post content. */
2222
excerpt?: string;
2323
/** */
24-
image?: string;
24+
image?: ImageMetadata | string;
2525

2626
/** */
2727
category?: string;

src/utils/blog.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { PaginateFunction } from 'astro';
12
import { getCollection } from 'astro:content';
23
import type { CollectionEntry } from 'astro:content';
34
import type { Post } from '~/types';
@@ -162,7 +163,7 @@ export const findLatestPosts = async ({ count }: { count?: number }): Promise<Ar
162163
};
163164

164165
/** */
165-
export const getStaticPathsBlogList = async ({ paginate }) => {
166+
export const getStaticPathsBlogList = async ({ paginate }: { paginate: PaginateFunction }) => {
166167
if (!isBlogEnabled || !isBlogListRouteEnabled) return [];
167168
return paginate(await fetchPosts(), {
168169
params: { blog: BLOG_BASE || undefined },
@@ -182,16 +183,16 @@ export const getStaticPathsBlogPost = async () => {
182183
};
183184

184185
/** */
185-
export const getStaticPathsBlogCategory = async ({ paginate }) => {
186+
export const getStaticPathsBlogCategory = async ({ paginate }: { paginate: PaginateFunction }) => {
186187
if (!isBlogEnabled || !isBlogCategoryRouteEnabled) return [];
187188

188189
const posts = await fetchPosts();
189-
const categories = new Set();
190+
const categories = new Set<string>();
190191
posts.map((post) => {
191192
typeof post.category === 'string' && categories.add(post.category.toLowerCase());
192193
});
193194

194-
return Array.from(categories).flatMap((category: string) =>
195+
return Array.from(categories).flatMap((category) =>
195196
paginate(
196197
posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
197198
{
@@ -204,16 +205,16 @@ export const getStaticPathsBlogCategory = async ({ paginate }) => {
204205
};
205206

206207
/** */
207-
export const getStaticPathsBlogTag = async ({ paginate }) => {
208+
export const getStaticPathsBlogTag = async ({ paginate }: { paginate: PaginateFunction }) => {
208209
if (!isBlogEnabled || !isBlogTagRouteEnabled) return [];
209210

210211
const posts = await fetchPosts();
211-
const tags = new Set();
212+
const tags = new Set<string>();
212213
posts.map((post) => {
213214
Array.isArray(post.tags) && post.tags.map((tag) => tags.add(tag.toLowerCase()));
214215
});
215216

216-
return Array.from(tags).flatMap((tag: string) =>
217+
return Array.from(tags).flatMap((tag) =>
217218
paginate(
218219
posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)),
219220
{

0 commit comments

Comments
 (0)