Getting my IDE to autocomplete Creep.prototype.something = function ()



  • Somehow it's been a real pain to get autocompletion and typescript up and running, but I have it. Visual Studio and Sublime Text know about and can 'go to definition' for all the external Screeps functions.

    But they somehow CAN'T figure out how to deal with Creep.prototype.doRole = function ()

    It doesn't autocomplete it, it doesn't jump to the definition, it's as if it wasn't there.

    Someone just tell me they have this working. This is really the last thing I need to actually get into this game.



  • I think the problem is that you can define Creep.prototype.something multiple times. VS can't know which definition you mean now. For example:

    // Some code that sets somethingSetting
    if (somethingSetting === 1) {
        Creep.prototype.something = function() {
            console.log("1");
        };
    } else {
        Creep.prototype.something = function() {
            console.log("0");
        };
    }
    
    myCreep.something(); // Which function is executed now?
    


  • What I do is set up a file called aaa.d.ts that contains type definitions for all my additional prototype functions.

    interface CreepMemory {
        task?: string;
        bodyType?: string;
        home?: string;
        storage?: string;
        working?: true;
        waypoint?: number;
    }
    
    interface BodyType {
        parts: BodyPartConstant[];
        create(amount: number): BodyPartConstant[];
    }
    
    interface LegacyTaskType<T> {
        body: string;
        canPerform(creep: Creep, target: T, running: boolean): boolean;
        spawnWeight: number;
        color: string;
        perform(creep: Creep, target: T): ScreepsReturnCode;
        heals?: boolean;
    }
    
    interface Room {
        creepTypes?: Record<string, number>;
        targetCreepTypes?: Record<string, number>;
        unassignedPoint?: RoomPosition;
    }
    
    interface StructureLink {
        receiving?: boolean;
        sent?: boolean;
    }
    
    interface Creep {
        moveColor?: string;
        task?: any;
    }
    
    interface Console {
        log(value: any, ...extra: any[]): void;
        commandResult(message: any): void;
    }
    
    declare namespace NodeJS {
        interface Global extends Record<string, any> {
            FIND_TOMBSTONES?: FIND_TOMBSTONES; // Workaround for PTR bug - probably fixed by now
        }
    }