Section 06 · 21 rules

Navigation, TypeScript & API layer

  • NV1 MUST keep navigation typed end to end: typed routes in Expo Router, static configuration (the setup the React Navigation docs recommend) with inferred types, or a param list when screens are dynamic. NEVER cast params with as any.
  • NV2 MUST, in Expo Router projects, import navigation APIs from expo-router. From SDK 56, importing @react-navigation/* packages directly in app code is not supported.
  • NV3 MUST pass only IDs and primitives as params (ST5) and validate them at screen entry. Params can arrive from a deep link, so they are user input.
  • NV4 MUST make every deep-linkable screen handle a missing or invalid param by redirecting, never by crashing.
  • NV5 MUST decide signed-in vs signed-out in one place (root layout or root navigator, using the router’s protected or conditional screens). NEVER redirect per screen from a useEffect.
  • NV6 NEVER call navigation from API or store code. Change state (for example, clear the session) and let NV5 route.
  • NV7 SHOULD keep navigator nesting shallow: one stack per tab, modals at the root, at most 3 levels.

TypeScript

  • TS1 MUST run with strict: true. New projects also turn on noUncheckedIndexedAccess.
  • TS2 NEVER silence the compiler with any, @ts-ignore or a non-null !. Use unknown and narrow it. @ts-expect-error with a reason counts as a guardrail exception.
  • TS3 MUST derive types from one source: an API type comes from its schema (z.infer<typeof Transfer>), never a hand-written copy beside it.
  • TS4 SHOULD use string-literal unions or as const objects instead of enum.
  • TS5 NEVER import another feature’s types except through its index.ts (S3).

API layer

  • AP1 MUST have one API client in shared/lib/api (base URL, auth header, timeout, error shape). Features call it only through fetchers in their api/ folder. NEVER fetch inside a component.
  • AP2 MUST use fetch unless the repo already uses another client (A2). NEVER add axios to a new project just for interceptors.
  • AP3 MUST validate responses with a schema at the boundary for every endpoint that touches money, auth or navigation; SHOULD for the rest. Use the repo’s schema library, or Zod in a new project. A failed parse is reported (EB6) and shown as the error state.
  • AP4 MUST turn every failure into one typed error shape ({ status, code, message }). UI branches on code, never on message text.
  • AP5 MUST set a timeout on every request and leave retries to the query library. NEVER auto-retry a non-idempotent mutation (payment, transfer); send an idempotency key if the backend supports one.
  • AP6 MUST refresh tokens in the client only, single-flight: concurrent 401s wait for one refresh. If the refresh fails, clear the session and let NV5 route.
  • AP7 NEVER put secrets in the app. Anything bundled, including EXPO_PUBLIC_* variables, is readable by users. Secret keys live on the server.
  • AP8 NEVER log request or response bodies containing tokens or personal data, including in internal test builds.
  • AP9 NEVER add a DTO-to-model mapper layer by default. Map in the fetcher only when the API shape and the app’s needs truly differ.
// features/transfer/api/transfer.ts: schema → type → fetcher → query options
export const Transfer = z.object({ id: z.string(), amount: z.number().int(), status: z.enum(['pending', 'done', 'failed']) });
export type Transfer = z.infer<typeof Transfer>;

const getTransfer = (id: string) => api.get(`/transfers/${id}`, Transfer); // client parses with the schema

export const transferQueries = {
  detail: (id: string) => queryOptions({ queryKey: ['transfer', id], queryFn: () => getTransfer(id) }),
};