Skip to content

Commit

Permalink
Fix: RoomObject.effects can be undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondMofeng committed Sep 15, 2023
1 parent ae52290 commit 2f426c0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fix: Mark `Spawning.directions` as optional ([#244](https://github.com/screepers/typed-screeps/pull/244))
- Fix: Unlimited stores have no notion of free capacity ([#208](https://github.com/screepers/typed-screeps/pull/208))
- Fix: `Room.getEventLog()` may return string ([#245](https://github.com/screepers/typed-screeps/pull/245))
- FIx: Mark `RoomObject.effects` as optional

### Removed

Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3759,7 +3759,7 @@ interface RoomObject {
/**
* Applied effects, an array of objects with the following properties:
*/
effects: RoomObjectEffect[];
effects?: RoomObjectEffect[];
/**
* An object representing the position of this object in the room.
*/
Expand Down
15 changes: 14 additions & 1 deletion dist/screeps-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function resources(o: GenericStore): ResourceConstant[] {
} else {
// Boost resource
const targetSource = Game.getObjectById("targetSourceID" as Id<Source>)!;
const sourceEffect = targetSource.effects.find((effect) => effect.effect === PWR_REGEN_SOURCE && effect.level > 0);
const sourceEffect = targetSource.effects?.find((effect) => effect.effect === PWR_REGEN_SOURCE && effect.level > 0);
if (!sourceEffect && powerCreep.powers[PWR_REGEN_SOURCE] && powerCreep.powers[PWR_REGEN_SOURCE].cooldown === 0) {
powerCreep.usePower(PWR_REGEN_SOURCE, targetSource);
}
Expand Down Expand Up @@ -1361,6 +1361,19 @@ function atackPower(creep: Creep) {
}
}

// Room Object
{
// `RoomObject.effects` can be undefined
{
const source = new Source("" as Id<Source>);
// @ts-expect-error
source.effects.find((effect) => effect.effect === PWR_REGEN_SOURCE);

// no error with optional chaining operator
source.effects?.find((effect) => effect.effect === PWR_REGEN_SOURCE);
}
}

// Id
{
/// @ts-expect-error
Expand Down
2 changes: 1 addition & 1 deletion src/room-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface RoomObject {
/**
* Applied effects, an array of objects with the following properties:
*/
effects: RoomObjectEffect[];
effects?: RoomObjectEffect[];
/**
* An object representing the position of this object in the room.
*/
Expand Down

0 comments on commit 2f426c0

Please sign in to comment.