Avoid using as to cast values (#1115)

## Description:

- Use `<argument> is <type>` return type declarations in favor of `as`.
- Use `satisfies` instead of `as`.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [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>
This commit is contained in:
Scott Anderson
2025-06-10 00:59:31 -04:00
committed by GitHub
parent a19bfec40a
commit cfbed15fad
4 changed files with 19 additions and 15 deletions
+2 -2
View File
@@ -12,7 +12,7 @@ import targetIcon from "../../../../resources/images/TargetIcon.svg";
import traitorIcon from "../../../../resources/images/TraitorIcon.svg";
import { PseudoRandom } from "../../../core/PseudoRandom";
import { Theme } from "../../../core/configuration/Config";
import { AllPlayers, Cell, nukeTypes, UnitType } from "../../../core/game/Game";
import { AllPlayers, Cell, nukeTypes } from "../../../core/game/Game";
import { GameView, PlayerView } from "../../../core/game/GameView";
import { UserSettings } from "../../../core/game/UserSettings";
import { createCanvas, renderNumber, renderTroops } from "../../Utils";
@@ -516,7 +516,7 @@ export class NameLayer implements Layer {
const isSendingNuke = render.player.id() === unit.owner().id();
const notMyPlayer = !myPlayer || unit.owner().id() !== myPlayer.id();
return (
(nukeTypes as UnitType[]).includes(unit.type()) &&
nukeTypes.includes(unit.type()) &&
isSendingNuke &&
notMyPlayer &&
unit.isActive()
+3 -1
View File
@@ -120,7 +120,9 @@ export class BotBehavior {
// Prefer neighboring bots
const bots = this.player
.neighbors()
.filter((n) => n.isPlayer() && n.type() === PlayerType.Bot) as Player[];
.filter(
(n): n is Player => n.isPlayer() && n.type() === PlayerType.Bot,
);
if (bots.length > 0) {
const density = (p: Player) => p.troops() / p.numTilesOwned();
let lowestDensityBot: Player | undefined;
+10 -8
View File
@@ -157,7 +157,7 @@ export class PlayerImpl implements Player {
troops: a.troops(),
id: a.id(),
retreating: a.retreating(),
} as AttackUpdate;
} satisfies AttackUpdate;
}),
incomingAttacks: this._incomingAttacks.map((a) => {
return {
@@ -166,7 +166,7 @@ export class PlayerImpl implements Player {
troops: a.troops(),
id: a.id(),
retreating: a.retreating(),
} as AttackUpdate;
} satisfies AttackUpdate;
}),
outgoingAllianceRequests: outgoingAllianceRequests,
hasSpawned: this.hasSpawned(),
@@ -252,7 +252,9 @@ export class PlayerImpl implements Player {
if (this.mg.map().isLand(neighbor)) {
const owner = this.mg.map().ownerID(neighbor);
if (owner !== this.smallID()) {
ns.add(this.mg.playerBySmallID(owner) as Player | TerraNullius);
ns.add(
this.mg.playerBySmallID(owner) satisfies Player | TerraNullius,
);
}
}
}
@@ -394,7 +396,7 @@ export class PlayerImpl implements Player {
if (this.isAlliedWith(recipient)) {
throw new Error(`cannot create alliance request, already allies`);
}
return this.mg.createAllianceRequest(this, recipient as Player);
return this.mg.createAllianceRequest(this, recipient satisfies Player);
}
relation(other: Player): Relation {
@@ -481,7 +483,7 @@ export class PlayerImpl implements Player {
.map((a) => a.other(this))
.flatMap((ally) => ally.targets());
ts.push(...this.targets());
return [...new Set(ts)] as Player[];
return [...new Set(ts)] satisfies Player[];
}
sendEmoji(recipient: Player | typeof AllPlayers, emoji: string): void {
@@ -1007,8 +1009,8 @@ export class PlayerImpl implements Player {
if (this.mg.owner(tile) === this) {
return false;
}
if (this.mg.hasOwner(tile)) {
const other = this.mg.owner(tile) as Player;
const other = this.mg.owner(tile);
if (other.isPlayer()) {
if (this.isFriendly(other)) {
return false;
}
@@ -1018,7 +1020,7 @@ export class PlayerImpl implements Player {
return false;
}
if (this.mg.hasOwner(tile)) {
return this.sharesBorderWith(this.mg.owner(tile));
return this.sharesBorderWith(other);
} else {
for (const t of this.mg.bfs(
tile,
+4 -4
View File
@@ -15,8 +15,8 @@ describe("getMessageTypeClasses", () => {
it("should return a valid CSS class for every MessageType", () => {
const messageTypes = Object.values(MessageType).filter(
(value) => typeof value === "number",
) as MessageType[];
(value): value is MessageType => typeof value === "number",
);
messageTypes.forEach((messageType) => {
const result = getMessageTypeClasses(messageType);
@@ -30,8 +30,8 @@ describe("getMessageTypeClasses", () => {
it("should not trigger console.warn for any MessageType", () => {
const messageTypes = Object.values(MessageType).filter(
(value) => typeof value === "number",
) as MessageType[];
(value): value is MessageType => typeof value === "number",
);
messageTypes.forEach((messageType) => {
getMessageTypeClasses(messageType);