screepers / typed-screeps

Strong TypeScript declarations for the game Screeps.
MIT License
189 stars 77 forks source link

Refactor find* methods. Part 1/3 : Decoupling `FilterOption` and `FindConstant` #235

Closed DiamondMofeng closed 1 year ago

DiamondMofeng commented 1 year ago

Part1: Decoupling FilterOption and FindConstant

TODO

This change would break nothing. For filters, if they had no error before, they would still be ok after this change. This means legacy problems would not get solved, for example, the below test cases can not be passed now and after this change. We'd hope to solve this in the next part.

// worse case, narrowing into incorrect type
// but it is on your own risk if you use `as` to cast the result
{
        {
            // @ts-expect-error
            const towers: StructureTower[] = creep.pos.findInRange(FIND_STRUCTURES, 2, {
                filter: (s) => s.structureType === STRUCTURE_EXTENSION,
            });
            towers[0].attack(creep);
        }
        {
            // @ts-expect-error
            const tower1: StructureTower | null = creep.pos.findClosestByPath(FIND_STRUCTURES, {
                filter: (s) => s.structureType === STRUCTURE_EXTENSION,
            });
            tower1?.attack(creep);
        }
        {
            // @ts-expect-error
            const tower2: StructureTower | null = creep.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (s) => s.structureType === STRUCTURE_SPAWN,
            });
            tower2?.attack(creep);
        }
        // using `as` on your own risk
        {
            // $ExpectType StructureTower[]
            const towers: StructureTower[] = creep.pos.findInRange(FIND_STRUCTURES, 2, {
                filter: (s) => s.structureType === STRUCTURE_EXTENSION,
            }) as StructureTower[];
            towers[0].attack(creep);
        }
}