Id<T>



  • RE: "@types/screeps": "^3.2.1",

    How does Game.getObjectById(id: Id < T>): T | null; work - how does one construct the id parameter? the obsolete warning of Game.getObjectById(id: string): T | null; is triggering one's PSTD;



  • Id<T> is basically an alias of a string. Usually, you can simply replace string with Id<T> wherever you need to.



  • @jbyoshi said in Id<T>:

    How?



  • The Id<T> type was added to assist situations like this simple example.

    interface CreepMemory { 
      sourceId: Id<Source>
    }
    
    let source = Game.getObjectById(creep.memory.sourceId)
    

    In this example, the type of source would be Source (or null) because the type of the game object can be known by the type of the Id.

    In this way, you don't need to use something like as Source to type assert it. Consider treating object Ids as something that is typed and not generic strings.



  • OK, let one rephrase one's question. Imagine one was storing a bunch of id-s, let's say for structures to be maintained, and imagine that this list would include walls and ramparts.
    How would one go about invoking the magicks of getObjectById of this list w/o incurring the wrath of tslint (pbuh)?



  • @adeptofnone said in Id<T>:

    tslint

    If I understand your question correctly the best solution would be to store two lists instead of one. First list/array to store all IDs and second list/array to store which structure types each ID belongs to. 1:1 relationship between indexes of both lists will be easy to maintain



  • @adeptofnone said in Id<T>:

    OK, let one rephrase one's question. Imagine one was storing a bunch of id-s, let's say for structures to be maintained, and imagine that this list would include walls and ramparts.
    How would one go about invoking the magicks of getObjectById of this list w/o incurring the wrath of tslint (pbuh)?

    The T in Id<T> can be a union type. So you can do something like:

    let id: Id<StructureWall | StructureRampart>;
    let wallOrRampart: StructureWall | StructureRampart | null = Game.getObjectById(id);
    

    You can read more about union types here: https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html



  • @Coelso Thanks, but calling getObjectById and using the said objects is not the problem. I have a problem with Game.getObjectById(id:string) giving me a warning, for it is marked obsolete. Surely there must be a way for constructing a proper Id parameter for this call from a string + list of known types?

    0_1604338241978_screenshot.png

      let dismantleObject:Structure<BuildableStructureConstant>|undefined;
    
                while(target.anchorRoom!.$roomCache.dismantle.ids.length > 0 && !dismantleObject){
                    try{
                        const id = target.anchorRoom!.$roomCache.dismantle.ids.shift()!;
                        dismantleObject = Game.getObjectById(id) as Structure<BuildableStructureConstant>;
                        if (dismantleObject){
                            target.anchorRoom!.$roomCache.dismantle.ids.unshift(id);
                        }
                    }catch(e){
                        this.logError(`ERROR ${e}`);
                    }
                }
    
                if (!dismantleObject){
                    return;
                }
    
                worker.moveTo(dismantleObject, {range: 1});
                dismantleObject.notifyWhenAttacked(false);
                const res = worker.dismantle(dismantleObject);
                this.log(`${worker.name} dismantle res:${res}`);


  • To fix that, you'll want to change the type of $roomCache.dismantle.ids. It looks like it's a string[] right now; if so, I think you need to change that to Id<Structure<BuildableStructureConstant>>[].



  • This post is deleted!