Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor find* methods. Part 2/3 : Ban random cast from results of find*() #236

Open
1 task
DiamondMofeng opened this issue Feb 26, 2023 · 0 comments
Open
1 task
Labels
BREAKING CHANGE A breaking change would be introduced enhancement

Comments

@DiamondMofeng
Copy link
Member

Part2: Ban random cast for results of find*()

!!!THIS IS A BREAKING CHANGE!!!

!!!THIS A BREAKING CHANGE!!!

This will not be easily merged. Feel free to give your idea on whether we should apply this to our next major version.

TODO

  • use overloads to distinguish narrow and non-narrow cases.

This change would ban random cast from results of find*(). If you do not use "type predicate filter", then you have to use as.

For example

{
        {
            // @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([] as AnyStructure[], {
                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);  //no Error. TS trusts you
        }
}

You can use "type predicate filter" to narrow the union type if you do not want to use as

const isStructureType = <T extends StructureConstant>(structureType: T) => {
    return (structure: AnyStructure): structure is ConcreteStructure<T> => {
        return structure.structureType === structureType;
    };
};

    {
        let towers = creep.pos.findInRange([] as AnyStructure[], 2, {
            filter: isStructureType(STRUCTURE_TOWER),
        });
        towers[0].attack(creep);    // ok
    }
    {
        let tower = creep.pos.findClosestByPath([] as AnyStructure[], {
            filter: isStructureType(STRUCTURE_TOWER),
        });
        tower?.attack(creep);    // ok
    }
    {
        let tower = creep.pos.findClosestByRange([] as AnyStructure[], {
            filter: isStructureType(STRUCTURE_TOWER),
        });
        tower?.attack(creep);    // ok
    }
@DiamondMofeng DiamondMofeng added enhancement BREAKING CHANGE A breaking change would be introduced labels Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE A breaking change would be introduced enhancement
Projects
None yet
Development

No branches or pull requests

1 participant