Without typed routes, the Href type accepts any string or HrefObject, so a misspelled path or a wrong param name compiles without complaint. Typed routes have Expo CLI generate types from the routes in your project, which makes Link, useRouter and the params hooks statically typed. TypeScript then reports an invalid href where you wrote it.
Key terms
- experiments.typedRoutes: the app config flag that tells Expo CLI to generate route types.
- Href: the type of the
hrefprop onLinkand of the argument torouter.push, a string path or an HrefObject. - expo-env.d.ts: a file Expo CLI generates in the project root, holding a reference to
expo/types.
The typedRoutes flag
Projects created from the default template already have typed routes: their app.json sets experiments.typedRoutes to true. The feature is still labeled beta, and Expo CLI only generates route types while that flag is set. In a project without it, add the flag yourself:
json{ "expo": { "experiments": { "typedRoutes": true } } }
Then run npx expo customize tsconfig.json, which adds the include entries the types need to tsconfig.json, and start the dev server with npx expo start. The types are generated when the dev server starts. If you set the flag back to false, Expo CLI removes the files it added.
What Expo CLI adds to the project
With the flag on, Expo CLI writes the generated types to .expo/types, creates expo-env.d.ts in the project root and adds it to .gitignore. It also adds expo-env.d.ts and .expo/types/**/*.ts to the include list in tsconfig.json. Leave those entries in place, and don't edit, delete or commit expo-env.d.ts.
Because the types live in git-ignored files, a fresh checkout has none of them. A CI job that type-checks without starting the dev server runs npx expo customize tsconfig.json first to generate them.
Hrefs TypeScript rejects
Components and functions that take an Href now get a much stricter type. In a project with src/app/products/[sku].tsx, a filled-in path, a template literal and an HrefObject with the right key all pass:
tsx<Link href="/products/blue-mug" /> <Link href={`/products/${sku}`} /> <Link href={{ pathname: '/products/[sku]', params: { sku: 'blue-mug' } }} />
Each line below is a TypeScript error:
tsx<Link href="/prodcuts/blue-mug" /> // error: not a valid route <Link href={{ pathname: '/products/blue-mug', params: { sku: 'blue-mug' } }} /> // error: no route file has this pathname <Link href={{ pathname: '/products/[sku]' }} /> // error: params are missing <Link href={{ pathname: '/products/[sku]', params: { id: 'blue-mug' } }} /> // error: sku is missing
The misspelled path matches no route. In the object form, pathname names the route file, brackets included, so a filled-in pathname fails even though the same text works as a string href. A dynamic route's HrefObject also needs params, holding the segment's key: with id in place of sku, the required sku is missing. router.push from useRouter() is typed too and checks its href the same way.
The check has gaps, so an href that compiles can still be wrong. The params type accepts any other string key as well, so params: { sku: 'blue-mug', size: 'L' } passes and size ends up in the query string. In a string href, any text can fill a dynamic segment, which means "/products/[sku]" compiles too. A string href is used as written, so nothing fills in sku. Relative hrefs such as "./blue-mug" type-check by their form alone, with no check that the destination exists.
Params typed from the route
Pass the route's full path as the generic to useLocalSearchParams or useGlobalSearchParams, and the params take their types from the route. A plain segment comes back as string and a catch-all as string[]. Most query params aren't in the file system, so they can't be typed automatically. Add them as a second generic, after the route, and the hook returns the route's params combined with that type. The form from the previous lesson, a single object type such as useLocalSearchParams<{ sort?: string }>(), keeps working next to it. The HelpArticle screen below does both for src/app/help/[...topic].tsx.
topic is a string[] because of [...topic], so topic.join(' / ') type-checks without a cast. lang comes from the second generic, { lang?: string }, and is string | undefined, which is why the screen falls back to 'en'. The Link at the bottom uses the object form that passes the stricter Href type for a dynamic route.
Code Examples
import { Link, useLocalSearchParams } from 'expo-router';
import { Text, View } from 'react-native';
export default function HelpArticle() {
const { topic, lang } = useLocalSearchParams<'/help/[...topic]', { lang?: string }>();
return (
<View>
<Text>{topic.join(' / ')}</Text>
<Text>Language: {lang ?? 'en'}</Text>
<Link href={{ pathname: '/products/[sku]', params: { sku: 'blue-mug' } }}>
Shop the blue mug
</Link>
</View>
);
}