# Decisions — `@codefast/typescript-config`

## Charter

**Problem.** Every package in a monorepo needs the same strict compiler settings, held to the runtimes its code ships
to, without each `tsconfig.json` restating them.

**Goals.**

- One strict, bundler-first baseline, with small presets for libraries, React and Next.js and an overlay for `tsc`
  builds.
- `lib` and `target` at the newest ECMAScript edition the [Node.js and browser floors](../../SUPPORT.md) both ship, so a
  builtin past the floor is a type error.
- Plain JSON with no runtime code; a project overrides any option locally.

**Non-goals.**

- Emitting: the presets type-check, and only the build overlay turns emit on.

The decisions below on TypeScript and subpath imports hold repo-wide; they live here because this package ships the
compiler settings every workspace extends.

## TypeScript 7 is every package's consumer floor

**Status:** Accepted

**Context.** TypeScript 7 alone emits the published `.d.ts` files, and is the one compiler the repo builds and
type-checks every package with.

**Decision.** The [TypeScript floor](../../SUPPORT.md#typescript) of every `@codefast/*` package is the compiler the
repo builds with, never the oldest release that happens to parse the declarations. This package declares it as its
`typescript` peer range.

**Consequences.** A `typescript` major bump raises the floor in the same change.

## A build loads only its runtime's ambient types

**Status:** Accepted

**Context.** An ambient type a program loads makes its globals legal everywhere in it, so a Node global, or explicit
resource management, can slip into code that runs in a browser that lacks it.

**Decision.** No preset loads `@types/node` or lists `ESNext.Disposable`, and `library-build.json` sets `types: []`. A
package whose code runs on Node sets `types: ["node"]` in its build config; a browser or universal package leaves it
empty. Explicit resource management stays out except where a package's API names it — `@codefast/di` and
`@codefast/di-testing` list `ESNext.Disposable` in their own `lib`.

**Consequences.** A Node global in browser or universal source fails the build, and test-only ambient types stay out of
every build.

## Internal imports use a bare `#` prefix

**Status:** Accepted

**Context.** The published `dist/*.js` ships `#` specifiers verbatim for a consumer's Node to resolve, and Node's ESM
resolver rejects a `#/`-prefixed specifier with `ERR_INVALID_MODULE_SPECIFIER` before 24.14, while the in-repo runners
resolve `#/` themselves and hide it.

**Decision.** Internal subpath imports use a bare `#` — `#components/button`, `#tests/…` — and the `imports` keys are
`#*`, `#tests/*` and `#examples/*` to match.

**Consequences.** A package resolves on any Node 24 a consumer installs it on, since npm does not enforce `engines` by
default. `codefast audit:publish` reports a `#/` specifier before it ships.

## `#` resolves through `package.json#imports`, never `paths`

**Status:** Accepted

**Context.** `tsc`, Vite, Vitest and `tsx` all resolve `#` through the `imports` field, and the field is what a
consumer's tools read once the package leaves the repo; a `paths` mapping would be a second statement of the same map.

**Decision.** No `tsconfig` declares `paths` for internal aliases, and neither Vite nor Vitest sets
`resolve.tsconfigPaths`. Every `#*` source target lists its extension candidates — `./src/*` first, then `./src/*.ts`,
`./src/*.tsx`, `./src/*/index.ts`, `./src/*/index.tsx` — because `tsc` does no extension substitution on a bare
`./src/*`. Each `packages/*` tsconfig sets `customConditions: ["source"]`, because TypeScript's default conditions match
the `types` lane and would type-check `src/` against stale `dist/*.d.ts`.

**Consequences.** Cross-package imports resolve to the other package's `src`, so type-checking needs no prior build.
`apps/*` and `benchmarks/*` set no custom condition and consume the built `dist/`. `#styles.css?url` resolves on the
first candidate alone, which is why `./src/*` stays first.

## The three conditions are three audiences

**Status:** Accepted

**Context.** `tsc` emits `#` specifiers verbatim into `.d.ts` and `.js`, so the `imports` field keeps resolving after
the package is installed.

**Decision.** `source` → `./src/*` is the repo during development; `types` → `./dist/*.d.ts` is a consumer's `tsc`
resolving a `#` inside the shipped declarations; `default` → `./dist/*.js` is a consumer's Node resolving a `#` inside
the shipped JS. The two `dist` lanes never point at `src`.

**Consequences.** A consumer never type-checks or runs unbuilt source. `tsc --noEmit --traceResolution` prints the
condition a `#` specifier matched.

## A fallback array belongs only in a lane no real Node reads

**Status:** Accepted

**Context.** Node takes the first array entry it can parse, never checks that the file exists, and never falls through,
so a missing first candidate throws `ERR_MODULE_NOT_FOUND`. `tsx` reads only the first candidate, applying its own
extension probing to it.

**Decision.** Only the `source` lane, read by `tsc` and Vite, which both probe, keeps candidates; `types` and `default`
are single strings. `apps/*` may keep a bare array, except where raw Node resolves the import — `vite.config.ts`, which
Vite externalises, spells the extension to hit the first candidate. `benchmarks/*` drop the two `index` candidates and
keep `src/` flat, one `<group>.ts` per module and never an `index.ts`.

**Consequences.** A `#` specifier landing on a directory in a benchmark would throw `ERR_UNSUPPORTED_DIR_IMPORT` at
bench time while type-checking stays green, so the flat layout is what keeps it from arising.

## The development condition is named `source`

**Status:** Accepted

**Context.** Vite and webpack enable `development` on their own in dev mode, so a package exporting raw `.tsx` under it
would hand that source to every consumer: `next dev` fails with `Unknown module type` while `next build` stays green.

**Decision.** The condition that points at `src` is `source`, which no tool enables unless asked.

**Consequences.** Consumers resolve `dist` in every mode; only a toolchain that opts into `source` reads `src`.

## License

Released under the [MIT License](./LICENSE).
