fix: infer type of getStaticPaths

This commit is contained in:
marshmallow 2024-05-07 11:34:27 +10:00
parent 610fa12723
commit e6faf8ca98
8 changed files with 28 additions and 12 deletions

View file

@ -2,10 +2,11 @@
interface Props {}
import { useTranslations } from "../i18n/utils";
import type { Params } from "../i18n/utils";
import { Image } from "astro:assets";
import aux from "../../public/aux.svg";
const { lang } = Astro.params;
const { lang } = Astro.params as Params;
const translation = useTranslations(lang);
---

View file

@ -1,7 +1,8 @@
---
import { useTranslations } from "../../i18n/utils";
import type { Params } from "../../i18n/utils";
const { lang } = Astro.params;
const { lang } = Astro.params as Params;
const translation = useTranslations(lang);
---

View file

@ -2,8 +2,9 @@
import { useTranslations } from "../../i18n/utils";
import { Image } from "astro:assets";
import aux from "../../../public/aux.svg";
import type { Params } from "../../i18n/utils";
const { lang } = Astro.params;
const { lang } = Astro.params as Params;
const translation = useTranslations(lang);
---

View file

@ -1,7 +1,8 @@
---
import { useTranslations } from "../../i18n/utils";
import type { Params } from "../../i18n/utils";
const { lang } = Astro.params;
const { lang } = Astro.params as Params;
const translation = useTranslations(lang);
---

View file

@ -1,7 +1,8 @@
---
import { useTranslations } from "../../i18n/utils";
import type { Params } from "../../i18n/utils";
const { lang } = Astro.params;
const { lang } = Astro.params as Params;
const translation = useTranslations(lang);
---

View file

@ -1,7 +1,21 @@
import { ui, defaultLang } from "./ui";
import { ui, defaultLang, languages } from "./ui";
import type {
InferGetStaticParamsType,
InferGetStaticPropsType,
GetStaticPaths,
} from "astro";
export function useTranslations(lang: keyof typeof ui) {
return function t(key: keyof (typeof ui)[typeof defaultLang]) {
return ui[lang][key] || ui[defaultLang][key];
};
}
export const getStaticPaths = (async () => {
return Object.keys(languages).map((name) => ({
params: { lang: name as keyof typeof languages },
}));
}) satisfies GetStaticPaths;
export type Params = InferGetStaticParamsType<typeof getStaticPaths>;
export type Props = InferGetStaticPropsType<typeof getStaticPaths>;

View file

@ -1,12 +1,13 @@
---
import Header from "../components/Header.astro";
import type { Params } from "../i18n/utils";
interface Props {
title: string;
}
const { title } = Astro.props;
const { lang } = Astro.params;
const { lang } = Astro.params as Params;
---
<!doctype html>

View file

@ -1,11 +1,7 @@
---
import { languages } from "../../i18n/ui";
import Layout from "../../layouts/Layout.astro";
import Home from "../../components/home/Home.astro";
export async function getStaticPaths() {
return Object.keys(languages).map((name) => ({ params: { lang: name } }));
}
export { getStaticPaths } from "../../i18n/utils";
---
<Layout title="auxolotl.org">