Coder Social home page Coder Social logo

typed-screeps's Introduction

typed-screeps

Strong TypeScript declarations for the game Screeps.

Travis

Installation

The type definitions are published on DefinitelyTyped. To install them, run the following.

# npm
npm install @types/screeps

# yarn
yarn add @types/screeps

Breaking Changes:

  • Memory is typed by default. The added typings are:

    • CreepMemory
    • FlagMemory
    • SpawnMemory
    • RoomMemory

    If you like the idea of typed memory, but aren't ready to just jump fully in, you only need to make sure you define an interface for the above four types. Then you can extend them at a later time.

    Example:

    interface CreepMemory { [name: string]: any };
    interface FlagMemory { [name: string]: any };
    interface SpawnMemory { [name: string]: any };
    interface RoomMemory { [name: string]: any };

If you don't want to add types to the global Memory object, you will need to add the following interface along with the four above.

Example:

interface Memory { [key: string]: any };
  • Any place in code that uses a constant (ex STRUCTURE_EXTENSION or FIND_MY_SPAWNS is now constrained to use literal types. Here is the list of the new types:

    BodyPartConstant
    BuildableStructureConstant (this is a subset of StructureConstant)
    StructureConstant
    FindConstant
    LookConstant
    DirectionConstant
    ResourceConstant
    MineralConstant (this is a subset of ResourceConstant)
    ColorConstant
    ScreepsReturnCode
    Terrain
    

    To update your code, you just need to change any string types to match one of the above. For example, if your code had:

    function getBody(): string[] {
      return [ WORK, MOVE, CARRY ];
    }

    Change it to:

    function getBody(): BodyPartConstant[] {  // this line changed
      return [ WORK, MOVE, CARRY ];
    }
  • Some original functions were incorrectly typed to not include null as a possible return. You may need to update your code to reflect this update (ex. findClosestByPath or findClosestByRange)

Additional (non-breaking) Features:

  • ConstructionSite can be optionally constrained by a structure type (ex. ConstructionSite<STRUCTURE_CONTAINER>). TypeScript will enforce that the type property of the ConstructionSite appropriately matches
  • Resource can optionally be constrained (ex. Resource<RESOURCE_ENERGY>)
  • Mineral can optionally be constrained by MineralConstant (ex. Mineral<RESOURCE_GHODIUM>)
  • Structure can optionally be constrained (ex Structure<STRUCTURE_SPAWN | STRUCTURE_EXTENSION>)
  • Screeps classes derived from Structure (ex StructureContainer) have their type property correspondingly constrained
  • LookAt results are now constrained to the type looked for
  • Results from Find-type functions are now constrained to have a RoomPosition
  • Typings for new RawMemory and RoomVisuals
  • New union type AnyCreep to represent Creep and PowerCreep

Contribute

Issues and Pull Requests are welcome! Please read the Contributing Guidelines and Code of Conduct beforehand.


Workarounds / Caveats

Due to some unresolved issues in TypeScript, a few parts of the API can't currenty be typed perfectly without tradeoffs.

Below is a list (feel free to open an issue if you have any ideas, or wish to discuss):

  • The API returned from store or carry (ex. myContainter.store) returns an object with optional keys for each Resource Type, but is guaranteed to have a key for RESOURCE_ENERGY. This is currently not (perfectly) typable in TypeScript (see issues #13573 and #12215). The chosen workaround is to just manually list the types using a fake type _ResourceConstantSansEnergy

typed-screeps's People

Contributors

resir014 avatar arcath avatar nhanho avatar bryanbecker avatar markosulamagi avatar anisoptera avatar valerian avatar kotarou avatar pyrodogg avatar dmarcuse avatar pmoehl avatar dessix avatar jyarbro avatar ezolenko avatar wtfrank avatar bonzaiferroni avatar bastianh avatar chriskitching avatar mschultheiss83 avatar seancl avatar aemino avatar sheeo avatar lf- avatar tprk77 avatar remz-jay avatar jomik avatar caleywoods avatar bencbartlett avatar tcdejong avatar spacerecycler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.