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

Should getObjects() return a union of concrete types? #7

Open
ngbrown opened this issue Jul 4, 2021 · 3 comments
Open

Should getObjects() return a union of concrete types? #7

ngbrown opened this issue Jul 4, 2021 · 3 comments

Comments

@ngbrown
Copy link
Contributor

ngbrown commented Jul 4, 2021

/**
* Get all objects in the game.
*/
export function getObjects(): GameObject[];

The current return type of GameObject[] doesn't avail its self of being able to discriminate into specific objects. For example, I would expect the following to work:

  for (const o of getObjects()) {
    if ('body' in o && o.my) {
      o.attack(enemy)
    }
  }

But it doesn't because TypeScript doesn't assume that an array of a base type could be any implementation of it. I propose that a union type be created of all the items that could be returned and getObjects() returns that type of array.

type AllGameObjects =
  | GameObject
  | Creep
  | Structure
  | OwnedStructure
  | StructureTower
  | StructureSpawn
  | StructureContainer
  | StructureWall
  | StructureExtension
  | StructureRampart
  | ConstructionSite
  | Resource
  | Source
  | Flag
  | ScoreCollector;
export function getObjects(): AllGameObjects[]; 
@thmsndk
Copy link
Collaborator

thmsndk commented Jul 4, 2021

Sounds like a good idea, I guess that is what the types for Screeps: World does based on the FIND_XX constant you give the find methods. Not sure AllGameObjects should contain GameObject though?

Feel free to make a PR for the relevant changes :)

@lmik
Copy link
Member

lmik commented Sep 8, 2022

coming back to this, @ngbrown have you tested this? i am not sure about possible side effects of arena specific types being included in your union 🤔

@ngbrown
Copy link
Contributor Author

ngbrown commented Sep 8, 2022

Hi @lmik. It did work last time I tried it, but that was back in the closed alpha...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants