Enhance your Astro development experience with rigorous type safety for every route in your application. This integration automatically generates TypeScript definitions from your project's route structure.
Documentation β’ npm β’ GitHub
- πΎ Astro β Compatible with version 5 and 6 of Astro.
- π Typesafe β Ensures all URLs match a route in your Astro project.
- π Typed Link Component β Replace your anchor links with the typesafe
Linkcomponent. - π Typesafe Search Params β Support for typesafe and JSON serialized search params.
- π§© Custom Component Support β Add type safety to custom link components.
- π€Έ Zero Dependencies β No 3rd-party packages.
- Astro
^5.0.0or^6.0.0is required - Install Typescript and Astro Check
npm i -D typescript @astrojs/check- Add
astro checkcommand to build script inpackage.json
{
"scripts": {
"build": "astro check && astro build"
}
}- Add integration:
npx astro add astro-typesafe-routes- Start the Astro development server if it's not already running to run type generation:
npm run devIf automatic installation didnβt work, follow these steps:
- Install package:
npm install astro-typesafe-routes- Add integration to
astro.config.mjs:
import { defineConfig } from "astro/config";
import astroTypesafeRoutes from "astro-typesafe-routes";
export default defineConfig({
integrations: [astroTypesafeRoutes()],
});- Start the Astro development server if it's not already running to run code generation:
npm run devImport the Link component to create a typesafe link
---
import Link from "astro-typesafe-routes/link";
---
<Link to="/blog/[postId]" params={{ postId: "1" }}>Post</Link>To safely read route params with proper types, create a Route.
---
import { createRoute } from "astro-typesafe-routes/route-schema";
export const Route = createRoute({
routeId: "/blog/[postId]",
});
const params = Route.getParams(Astro);
------
import { createRoute } from "astro-typesafe-routes/create-route";
import { z } from "astro/zod";
export const Route = createRoute({
routeId: "/blog/[postId]",
});
export type Props = {
content: string;
};
export const getStaticPaths = Route.createGetStaticPaths<Props>(() => {
return [
{
params: {
postId: "1",
},
props: {
content: "Lorem Ipsum",
},
},
];
});
---
{Astro.props.content}For more in-depth information and detailed usage instructions, please refer to the documentation.
Astro Typesafe Routes will automatically update the routeId of changed routes. If you want to disable this, you can set disableAutomaticRouteUpdates in the integration options.
export default defineConfig({
integrations: [
astroTypesafeRoutes({
disableAutomaticRouteUpdates: true,
}),
],
});