Enable strictNullChecks, eqeqeq (#436)

## Description:

Improve type safety and runtime correctness by:
1. Enabling TypeScript's
[strictNullChecks](https://www.typescriptlang.org/tsconfig/#strictNullChecks)
compiler option.
2. Replacing all loose equality operators (`==` and `!=`) with strict
equality operators (`===` and `!==`).
3. Cleaning up of type declarations, null handling logic, and equality
expressions throughout the project.

Currently, the code allows implicit assumptions that `null` and
`undefined` are interchangeable, and relies on type-coercing equality
checks that can introduce subtle bugs. These practices make it difficult
to reason about when values may be absent and hinder the effectiveness
of static analysis.

Migrating to strict null checks and enforcing strict equality
comparisons will clarify intent, reduce bugs, and make the codebase
safer and easier to maintain.

Fixes #466 

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

---------

Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
Co-authored-by: evanpelle <openfrontio@gmail.com>
This commit is contained in:
Scott Anderson
2025-05-15 19:39:40 -04:00
committed by GitHub
parent 369483b4ac
commit 70745faac4
119 changed files with 1428 additions and 1123 deletions
+21 -11
View File
@@ -40,7 +40,7 @@ export class ParabolaPathFinder {
nextTile(speed: number): TileRef | true {
if (!this.curve) {
return;
throw new Error("ParabolaPathFinder not initialized");
}
const nextPoint = this.curve.increment(speed);
if (!nextPoint) {
@@ -72,14 +72,14 @@ export class AirPathFinder {
const ratio = Math.floor(1 + Math.abs(dstY - y) / (Math.abs(dstX - x) + 1));
if (this.random.chance(ratio) && x != dstX) {
if (this.random.chance(ratio) && x !== dstX) {
if (x < dstX) nextX++;
else if (x > dstX) nextX--;
} else {
if (y < dstY) nextY++;
else if (y > dstY) nextY--;
}
if (nextX == x && nextY == y) {
if (nextX === x && nextY === y) {
return true;
}
return this.mg.ref(nextX, nextY);
@@ -87,9 +87,9 @@ export class AirPathFinder {
}
export class PathFinder {
private curr: TileRef = null;
private dst: TileRef = null;
private path: TileRef[];
private curr: TileRef | null = null;
private dst: TileRef | null = null;
private path: TileRef[] | null = null;
private aStar: AStar;
private computeFinished = true;
@@ -111,12 +111,16 @@ export class PathFinder {
});
}
nextTile(curr: TileRef, dst: TileRef, dist: number = 1): TileResult {
if (curr == null) {
nextTile(
curr: TileRef | null,
dst: TileRef | null,
dist: number = 1,
): TileResult {
if (curr === null) {
consolex.error("curr is null");
return { type: PathFindResultType.PathNotFound };
}
if (dst == null) {
if (dst === null) {
consolex.error("dst is null");
return { type: PathFindResultType.PathNotFound };
}
@@ -134,7 +138,11 @@ export class PathFinder {
this.computeFinished = false;
return this.nextTile(curr, dst);
} else {
return { type: PathFindResultType.NextTile, tile: this.path.shift() };
const tile = this.path?.shift();
if (tile === undefined) {
throw new Error("missing tile");
}
return { type: PathFindResultType.NextTile, tile };
}
}
@@ -150,11 +158,13 @@ export class PathFinder {
return { type: PathFindResultType.Pending };
case PathFindResultType.PathNotFound:
return { type: PathFindResultType.PathNotFound };
default:
throw new Error("unexpected compute result");
}
}
private shouldRecompute(curr: TileRef, dst: TileRef) {
if (this.path == null || this.curr == null || this.dst == null) {
if (this.path === null || this.curr === null || this.dst === null) {
return true;
}
const dist = this.game.manhattanDist(curr, dst);
+1 -1
View File
@@ -123,7 +123,7 @@ export class SerialAStar implements AStar {
private expandTileRef(current: TileRef, isForward: boolean) {
for (const neighbor of this.gameMap.neighbors(current)) {
if (
neighbor != (isForward ? this.dst : this.closestSource) &&
neighbor !== (isForward ? this.dst : this.closestSource) &&
!this.gameMap.isWater(neighbor)
)
continue;