Skip to content

Commit b04ebdb

Browse files
authored
Merge pull request #12 from btravstack/feat/tsconfig-test-d
feat(tsconfig): `test-d.json`, the preset for a type-level test project
2 parents 1c37772 + 88a34ba commit b04ebdb

5 files changed

Lines changed: 97 additions & 3 deletions

File tree

‎.changeset/tsconfig-test-d.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
"@btravstack/tsconfig": minor
3+
---
4+
5+
`test-d.json`: the preset for a type-level test project.
6+
7+
Type-level tests assert with bindings nothing reads — `type _x = Expect<Equal<A, B>>`
8+
— so `noUnusedLocals` and `noUnusedParameters` must be off for that project and
9+
on everywhere else. Every workspace with a `tsconfig.test-d.json` was writing
10+
those two flags out by hand, along with the reason, or more often without it.
11+
12+
```json
13+
{
14+
"extends": ["./tsconfig.json", "@btravstack/tsconfig/test-d.json"],
15+
"include": ["src/**/*.test-d.ts"]
16+
}
17+
```
18+
19+
The preset comes **last** in the array so its relaxations win over the strict
20+
config beneath.
21+
22+
It carries no `include`, and cannot: TypeScript resolves a base config's
23+
`include` / `exclude` / `files` relative to the **base file's** own directory,
24+
so a shipped glob would point inside `node_modules` and match nothing. Measured
25+
— an `include` of `src/**/*.test-d.ts` in a base one directory up resolves to
26+
`../base/src/**/*.test-d.ts` and reports `TS18003`. `scripts/validate.mjs` holds the shape as an
27+
**allow-list** — `$schema` plus exactly those two `compilerOptions` — rather
28+
than forbidding the four keys a reader would reach for. The preset is layered
29+
last in a consumer's `extends` array, so anything it grows silently overrides
30+
the workspace's own config, and only naming what may appear catches a key
31+
nobody thought to forbid.

‎packages/tsconfig/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@ symbols came first, and the sentence naming the missing port came third. With
4747
A library keeps `base.json`: there the declaration check is the guarantee that
4848
its consumers can build.
4949

50+
## `test-d.json`, for a type-level test project
51+
52+
Type-level tests assert with bindings nothing reads — `type _x = Expect<Equal<A, B>>`
53+
— so `noUnusedLocals` and `noUnusedParameters` have to be off for that project
54+
and on everywhere else. `test-d.json` sets exactly those two flags — nothing
55+
else, which `scripts/validate.mjs` enforces as an allow-list — and composes
56+
with the workspace's own config through an `extends` array:
57+
58+
```json
59+
{
60+
"extends": ["./tsconfig.json", "@btravstack/tsconfig/test-d.json"],
61+
"include": ["src/**/*.test-d.ts"]
62+
}
63+
```
64+
65+
Order matters: the preset comes **last**, so its relaxations win over the
66+
strict config it is layered on.
67+
68+
It carries no `include`, and cannot. TypeScript resolves a base config's
69+
`include` / `exclude` / `files` relative to **the base file's own directory**
70+
(measured: an `include` of `src/**/*.test-d.ts` in a base one directory up
71+
resolves to `../base/src/**/*.test-d.ts` and reports `TS18003`), so a shipped
72+
preset's globs would point inside `node_modules`. Each workspace states its
73+
own — which is also where the answer belongs, since a workspace that keeps its
74+
type tests somewhere else needs a different glob, not a different preset.
75+
5076
It does **not** set `types` — TypeScript auto-includes every reachable
5177
`@types/*` package (Node included). This avoids forcing each consuming package to
5278
declare a direct `@types/node` just to satisfy a `types: ["node"]` list.

‎packages/tsconfig/package.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
},
2121
"files": [
2222
"app.json",
23-
"base.json"
23+
"base.json",
24+
"test-d.json"
2425
],
2526
"type": "module",
2627
"exports": {
2728
"./app.json": "./app.json",
28-
"./base.json": "./base.json"
29+
"./base.json": "./base.json",
30+
"./test-d.json": "./test-d.json"
2931
},
3032
"publishConfig": {
3133
"access": "public"

‎packages/tsconfig/test-d.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"compilerOptions": {
4+
"noUnusedLocals": false,
5+
"noUnusedParameters": false
6+
}
7+
}

‎scripts/validate.mjs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const check = (name, fn) => {
2222

2323
// Every package ships exactly the files it lists, and those files load.
2424
const shipped = {
25-
tsconfig: ["app.json", "base.json"],
25+
tsconfig: ["app.json", "base.json", "test-d.json"],
2626
typedoc: ["base.json"],
2727
oxlint: ["base.json"],
2828
oxfmt: ["base.json"],
@@ -58,6 +58,34 @@ check("tsconfig/app.json extends base and emits no declarations", () => {
5858
}
5959
});
6060

61+
check("tsconfig/test-d.json is exactly the two unused-check relaxations", () => {
62+
const testD = json("packages/tsconfig/test-d.json");
63+
// An ALLOW-list, not a deny-list: this preset is layered LAST in a consumer's
64+
// `extends` array, so anything it grows silently overrides the workspace's
65+
// own config. Naming what may appear is the only shape that catches a key
66+
// nobody thought to forbid.
67+
//
68+
// `include` / `exclude` / `files` are the ones a reader will reach for and
69+
// they cannot work here: TypeScript resolves a base config's globs relative
70+
// to the BASE file's own directory, so a shipped glob points inside
71+
// node_modules and matches nothing (measured: TS18003).
72+
const top = Object.keys(testD).sort();
73+
if (top.join() !== "$schema,compilerOptions") {
74+
throw new Error(`top-level keys must be $schema + compilerOptions, got: ${top.join(", ")}`);
75+
}
76+
const options = Object.keys(testD.compilerOptions ?? {}).sort();
77+
if (options.join() !== "noUnusedLocals,noUnusedParameters") {
78+
throw new Error(
79+
`compilerOptions must be exactly the two unused checks, got: ${options.join(", ")}`,
80+
);
81+
}
82+
// An assertion binding is never read, so both must be OFF — the whole reason
83+
// this file exists.
84+
for (const flag of options) {
85+
if (testD.compilerOptions[flag] !== false) throw new Error(`${flag} must be false`);
86+
}
87+
});
88+
6189
check("typedoc/base.json loads the markdown plugin", () => {
6290
const td = json("packages/typedoc/base.json");
6391
if (!td.plugin?.includes("typedoc-plugin-markdown")) {

0 commit comments

Comments
 (0)