TypeScript

PocketOne strips the types from .ts and .tsx files and runs them; it never checks them. To check them, use tsc or your editor with the types PocketOne writes for your app.

When the app starts, it writes types.d.ts next to pb_pages (in the project folder, beside pb_data and pocketone.yaml) with the declarations of the whole JS API, Pages included. Pages adds no globals: its types live in the pages namespace and you use them by name.

Set up

The app also writes a tsconfig.json there when you don't have one. It includes types.d.ts, pb_hooks, pb_migrations and pb_pages, with JSX checking on ("jsx": "preserve", so no React runtime is needed). Start the app once, then run npx tsc -p . or open the folder in your editor. Commit both files, so a fresh checkout is typed before the app first runs.

If you keep your own tsconfig.json, include types.d.ts and pb_pages/**/*, and set "jsx": "preserve" and "skipLibCheck": true (PocketBase's generated declarations have two type errors of their own).

The types

Type For
pages.Context ctx: req, res, data, params, route, auth, app and the helpers
pages.Handlers A page's .ts or an endpoint: load, get, post, put, patch, delete
pages.Middleware A +middleware.ts: (ctx, next) => ...
pages.Config +config.ts
pages.PluginFactory, pages.Plugin A custom plugin
pages.Props<L> A .tsx page, layout or partial; L types an include's locals

satisfies keeps your own return types and checks the shape:

// pb_pages/products/[id].ts
export default {
  async load(ctx) {
    ctx.data.product = await ctx.app.findRecordById.async('products', ctx.params.id)
  },
  post(ctx) {
    const { qty } = ctx.req.body<{ qty: number }>()
    return ctx.res.redirect(`/products/${ctx.params.id}`, { flash: `Added ${qty}` })
  },
} satisfies pages.Handlers
// pb_pages/admin/+middleware.ts
const admin: pages.Middleware = (ctx, next) => {
  if (!ctx.auth) return ctx.res.redirect('/login')
  return next()
}
export default admin
// pb_pages/_private/badge.tsx, rendered with ctx.include('badge', { label: 'new' })
export default function Badge({ label, asset }: pages.Props<{ label: string }>) {
  return <span class="badge"><img src={asset('star.svg')} />{label}</span>
}

A component you import and use as <Card /> gets only the props you pass it, not ctx or asset, so give it a props type of its own.

Your own data, plugins and helpers

ctx.data accepts any key. To type yours, or what a custom plugin adds to ctx, add to the interfaces in a .d.ts file of your own, say pb_pages/_private/types.d.ts (in _private/, so it isn't served like other files in pb_pages):

declare namespace pages {
  interface Data {
    product?: core.Record
    section?: string
  }
  interface Context {
    hello(): string // added by your plugin's extendContext
  }
}

ctx.session and ctx.realtime are typed as always there; at runtime they exist only when the auth and realtime plugins are on in +config.ts.