# TypeScript

Neutron is written in TypeScript and is designed to provide excellent type safety out of the box.

## `tsconfig.json`

A scaffolded project ships a ready-to-use `tsconfig.json`. The options Neutron
relies on are Preact JSX and bundler module resolution:

```json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "lib": ["ES2022", "DOM"],
    "jsx": "react-jsx",
    "jsxImportSource": "preact",
    "strict": true,
    "skipLibCheck": true,
    "types": ["vite/client"]
  },
  "include": ["src", "src/**/.neutron-*.d.ts"]
}
```

The `include` glob picks up Neutron's generated type files
(`.neutron-routes.d.ts`, `.neutron-content.d.ts`), which are written on
`neutron-ts dev` / `neutron-ts build`. Add path aliases under
`compilerOptions.paths` if you want them — e.g. `"~/*": ["src/*"]`.

## Type Inference

Neutron's "magic" comes from its ability to infer types across the network boundary.

```tsx
// The return type of this function...
export async function loader() {
  return { hello: "world" };
}

// ...is inferred here!
const data = useLoaderData<typeof loader>();
// data is { hello: string }
```

This works for `useLoaderData`, `useActionData`, and generally means you rarely need to write manual interfaces for your API responses.
