Code style
How TypeScript is written in Callis. Biome handles formatting and the lint rules it supports (configured in T0000). This guide covers what a tool cannot check. When the guide and the linter disagree, fix whichever is wrong in the same PR.
Files and modules
- ESM only, with named exports only. No default exports.
- File names are
kebab-case.ts. Each file holds one main concept; tests sit next to it askebab-case.test.ts. - No barrel files inside
src/. The onlyindex.tsis the package entry point, which lists the public API explicitly. Internal barrels hide dependencies, create cycles and defeat tree-shaking. - Respect the module dependency rules in architecture.md. Imports between files must not form cycles. The modules
functionsandfhirimport each other without one:functions/registry.tsimportsfhir, andfhirimports onlyfunctions/define.ts, which does not importfhir. - Use
import typeandexport typefor type-only imports and exports.
TypeScript
-
Strict compiler settings come from
tsconfig.base.json(T0000). Do not loosen them per file. -
Erasable syntax only: no
enum,namespace, parameter properties or decorators. Use string-literal unions oras constobjects instead. Code then works with plain type stripping (Node.js, Deno, bundlers) and stays easy to read.ts// Instead of enum Precision { Year, Month, … }export type Precision = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'; -
Explicit return types on exported functions.
isolatedDeclarationsrequires them, and they document the API. -
unknown, notany. Ifanyis unavoidable, add a comment explaining why. -
Narrow instead of asserting. Avoid
!andas. Use@ts-expect-errorwith a reason; never@ts-ignore. -
Model variants as discriminated unions with a
kindfield, and endswitchstatements over them with an exhaustiveness check:tsswitch (node.kind) {case 'Literal':return compileLiteral(node);// …one case per kinddefault:return assertNever(node); // compile error if a kind is missing} -
readonlyfor data that crosses module boundaries: collections, AST nodes, model data.
Naming
PascalCasefor types, interfaces and classes;camelCasefor functions, variables and constants.- Functions are verbs (
parseDate,compileNode). Booleans read as questions (isEmpty,hasValue). - FHIRPath names keep the spec's spelling:
ofType,convertsToInteger,$this. - Error codes are
area/kebab-casestrings, such aseval/singleton-expected(error codes). - No abbreviations beyond the common ones (
ctx,expr,ast,fn).
Functions, data and state
- Prefer pure functions over plain data. Use classes for value types with invariants (
Decimal,DateTimeValue) and for hot-path objects that benefit from a stable shape (ModelNode). - Never mutate inputs or returned collections.
- No mutable module-level state. Compiled expressions must stay reentrant and safe for concurrent evaluations.
- Keep hot paths lean (avoid per-item closures and intermediate arrays), but optimize only with a benchmark to back it (T0033).
Errors
- Library code throws only
FhirPathErrorsubclasses with a stable code. Never throw strings or a bareError. - When the spec says the result is empty, return an empty collection. Do not throw.
- Do not catch and swallow errors. Catch only to add context, and rethrow.
- Messages are for people: say what was expected and what was found. Tests assert on codes, not on message text.
Async
- Follow ADR-0004: use the
MaybePromisehelpers, and detect promises withisThenable(), neverinstanceof Promise. - No floating promises. Every promise is awaited or returned.
Comments and documentation
- TSDoc on every export. Use
@seewith a spec link for spec behaviour, and@examplewhere it helps. - Comments explain why. Link the spec anchor for rules that are not obvious from the code.
- No commented-out code. A
TODOcarries a task reference:// TODO(T0033): cache type lookups.