Skip to content

Enable strictNullChecks (types only) - #82

Open
96jonesa wants to merge 3 commits into
masterfrom
andy/claude/strict-null-checks
Open

96jonesa wants to merge 3 commits into
masterfrom
andy/claude/strict-null-checks

Conversation

@96jonesa

@96jonesa 96jonesa commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #73.

What this does

Turns on strictNullChecks in tsconfig.json. test/tsconfig.json inherits it, so npm run typecheck in CI now checks null handling in src/ and test/.

This PR changes types only. Every edit is a type annotation, !, null!, let x!: T or as HTMLElement. TypeScript removes all of these when it compiles, so built/ is byte-for-byte identical. Check: rm -rf built && npm run build && git status built/ shows nothing. CI's built-is-up-to-date step checks the same thing.

The diff replaces 134 lines in place, plus one new line in tsconfig.json. Line numbers don't move, so master links in the follow-up issues stay accurate.

How each error was fixed

Kind Count Fix
Fields that really can be null (initialised or assigned null) 19 T | null
Functions that return null for invalid input (parseCannonInput, parseMovementsInput, parseFoodCallsInput, parseRunnerMovementsToCheck, convertCommandsStringToMap, getControlledPlayerObject), plus the locals that hold their result before a null check 21 lines T | null
x === null ? null : x.clone() in clone() for fields that are never null while the sim runs 24 null! in that branch
DOM lookups in sim.ts 17 lines as HTMLElement, the same idiom sim.ts already uses. All 15 ids are present in index.html.
Call sites the compiler can't prove are non-null 30 lines ! (see below)
Tests that force fields to null to cover the clone() branches 16 null!
Tests that call parseCannonInput with valid input 6 ! (the existing not.toBeNull() checks still run)

I didn't make position, destination, map, the five players and similar fields nullable. Nothing sets them to null while the sim runs. Only the defensive clone() branches and the tests that cover them do. Making them T | null would put a ! on almost every read of position.

The ! sites

Each ! claims the value can't be null there. Running the code can't show that, so an independent reader re-derived each claim from the code without seeing my reasons:

Site Why it's not null Verdict
BarbarianAssault.ts:320, 325, 328, 351, 354 prePositions maps are set for every player before the read agreed
BarbarianAssault.ts:561, 604, 647, 690, 733, sim.ts:2659 get() right after has() agreed
HealerCodeAction.ts:62, 74, 79 key set just before; groupOrder only holds set keys agreed
HealerPenance.ts:421, 440 only called after instanceof checks on target agreed
Player.ts:269 (let currentPosition!) the BFS queue starts non-empty, so the loop sets it agreed
Renderer.ts:25 getContext("2d") on the page's <canvas> agreed
RunnerPenance.ts:57 diedThisTick is always false there, so this branch never runs agreed → #80
sim.ts:2527 the whole string matched (h\d+,\d+)(-h\d+,\d+)+ agreed
sim.ts:473-477 (reset()) can be null, but nothing reads it yet → #79
sim.ts:2737-2741 (Simulate) can be null and crashes → #79

The two sim.ts cases keep their ! so behaviour is unchanged. #79 fixes them for real, and its fix lets those ! be removed.

Verified

  • npm run typecheck is clean, and all 295 tests pass.
  • built/ is identical after a clean rebuild.
  • The Simulate crash is reproduced: a BarbarianAssault with a null command map throws on its first tick().

Found along the way

Not included

🤖 Generated with Claude Code

96jonesa and others added 3 commits October 27, 2026 23:55
Nullable fields become T | null. Other sites use ! / null! / definite
assignment, all of which are erased, so built/ is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enable strictNullChecks

1 participant