No description
Find a file
Etienne Samson af9a74e8ae
Some checks failed
CI / build (18.x) (push) Has been cancelled
CI / build (20.x) (push) Has been cancelled
CI / build (22.x) (push) Has been cancelled
Update the Game.market.calcTransactionCost formula (#277)
The [engine](97c9d12385/src/utils.js (L657C9-L659C3)) formula,
or the [docs](https://docs.screeps.com/api/#Game.market.calcTransactionCost)
2025-08-05 10:30:52 +08:00
.github Update dependencies, replace tslint with eslint, migrate to pnpm (#259) 2024-07-20 22:16:37 +08:00
.husky Finish updating Husky 2022-04-16 21:27:47 +03:00
.vscode Update dependencies, replace tslint with eslint, migrate to pnpm (#259) 2024-07-20 22:16:37 +08:00
build Format concat.js (#263) 2025-01-31 18:32:40 +08:00
dist Update the Game.market.calcTransactionCost formula (#277) 2025-08-05 10:30:52 +08:00
src Update the Game.market.calcTransactionCost formula (#277) 2025-08-05 10:30:52 +08:00
.editorconfig fixed json file formatting 2018-01-15 19:03:55 +07:00
.gitattributes Ensure all files have LF linebreaks 2017-11-05 14:19:38 +07:00
.gitignore updated changelog + gitignore 2018-06-24 16:31:05 +07:00
.npmignore Update dependencies, replace tslint with eslint, migrate to pnpm (#259) 2024-07-20 22:16:37 +08:00
.prettierrc Setup CI using github action (#232) 2023-02-26 14:38:38 +08:00
CHANGELOG.md Update changelog. 2024-07-20 22:57:08 +08:00
CODE_OF_CONDUCT.md move contributing guidelines and code of conduct into root directory 2018-10-01 14:37:45 +07:00
CONTRIBUTING.md update test file location 2020-02-02 00:17:15 +01:00
eslint.config.mjs Update dependencies, replace tslint with eslint, migrate to pnpm (#259) 2024-07-20 22:16:37 +08:00
eslint.config.test.mjs Update dependencies, replace tslint with eslint, migrate to pnpm (#259) 2024-07-20 22:16:37 +08:00
LICENSE Include initial year in LICENSE 2017-11-17 16:39:07 +07:00
package.json Adapt to new DefinitelyTyped layout 2024-08-03 16:18:08 +08:00
pnpm-lock.yaml Update dependencies, replace tslint with eslint, migrate to pnpm (#259) 2024-07-20 22:16:37 +08:00
README.md Update dependencies, replace tslint with eslint, migrate to pnpm (#259) 2024-07-20 22:16:37 +08:00
tsconfig.json Update dependencies, replace tslint with eslint, migrate to pnpm (#259) 2024-07-20 22:16:37 +08:00

typed-screeps

Strong TypeScript declarations for the game Screeps: World.

Github action npm

Installation

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

# npm
npm install @types/screeps

# yarn
yarn add @types/screeps

# pnpm
pnpm add @types/screeps

Differences from Screeps-Typescript-Declarations

This repo has more activity and is considerably more up-to-date.

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)

  • Game.getObjectById() now returns typed objects according to the type of the Id (ex. Id<StructureTower> returns StructureTower type).

    Use of string typed Id, is deprecated and may be removed in the future. When using string Ids, the type of the returned game object is unknown which requires manual type assertion. Previously this function returned any typed objects which could accidently be left untyped;

    If you have code like this (un-type-asserted use of Game.getObjectById)

    interface Memory{
      towerIds: string[];
    }
    
    Memory.towerIds.forEach((towerId) => {
      const tower = Game.getObjectById(towerId); // type of returned tower is 'unknown' instead of 'any'
      tower.attack(targetCreep); // Error Object is of type unknown ts(2571)
    })
    

    Change it store typed Ids:

    interface Memory{
      towerIds: Array<Id<StructureTower>>;
    }
    
    Memory.towerIds.forEach((towerId) => {
      const tower = Game.getObjectById(towerId); // type of returned tower is StructureTower
      tower.attack(targetCreep);
    })
    

    If you're already manually asserting the type of the game object, you're not required to change anything immediately however this is deprecated and may be removed in the future.

    const towerId: Id<StructureTower> = ""  as Id<StructureTower>;
    const tower1 = Game.getObjectById(towerId); // recommended use, returns StructureTower type
    const tower2 = Game.getObjectById<StructureTower>(""); // @deprecated returns StructureTower type
    const tower3 = Game.getObjectById("") as StructureTower; // @deprecated returns StructureTower type
    

    Id<T> types are assignable to string but the reverse is not allowed implicitly. To assign a string type to an Id<T> type, you must explicitly assert the type.

    const typedId: Id<Creep> = "123" as Id<Creep>; // assertion required
    const untypedId1: string = typedId; // no assertion required
    
  • Game objects have typed id properties id: Id<this>. These typed ids can by passed to Game.getObjectById() to receive typed game objects matching the type of the Id. See above bullet for more details.

    creep.id // has type Id<Creep>
    copy = Game.getObjectById(creep.id) // returns type Creep
    tower.id // has type Id<StructureTower>
    

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.