• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Strong TypeScript declarations for the game Screeps.

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

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.

More Repositories

1

screeps-typescript-starter

Starter kit for TypeScript-based Screeps AI codes.
TypeScript
440
star
2

screeps-profiler

This is a profiler designed for use in the game of screeps.
JavaScript
193
star
3

screeps-launcher

Launcher for the Screeps Private Server
Go
119
star
4

screeps-multimeter

The most useful tool on your screeps workbench.
JavaScript
89
star
5

python-screeps

Unofficial client for the Screeps Unofficial API
Python
82
star
6

screeps-snippets

This repository is for saving pinned snippets from the screeps slack for posterity
JavaScript
74
star
7

Screeps-Typescript-Declarations

The repository for *Screeps's* TypeScript type definitions.
TypeScript
69
star
8

node-screeps-api

Node.js API For screeps
JavaScript
64
star
9

screeps-arena-typescript-starter

JavaScript
58
star
10

screeps_console

Standalone Interactive Screeps Console
Python
57
star
11

cppreeps

WASM C++ Screeps API and utilities pack (beta, proof-of-concepts)
C++
55
star
12

screeps-server-mockup

Private server package for unit tests
TypeScript
51
star
13

RoomVisual

JavaScript
39
star
14

screeps-typescript-profiler

A light-weight TypeScript profiler for tagging classes and methods
TypeScript
38
star
15

screeps-stats

Access Screeps Console, Performance, and Statistics Data via Kibana and ElasticSearch
Python
38
star
16

screeps-visual

Chrome Extension that adds a Visual API to screeps
JavaScript
26
star
17

typed-screeps-arena

JavaScript
26
star
18

screeps_notify

Send messages (SMS, Slack) from inside Screeps Scripts
Python
23
star
19

screeps-ConsoleStats

Output stats directly to the screeps console
JavaScript
18
star
20

creeptalk

Giving Creeps Social Skills Since 2016
JavaScript
17
star
21

POSIS

Portable Operating System Interface for Screeps
12
star
22

screepers-standards

12
star
23

utf15

Library (codec generator) for packing integers to JavaScript UTF-16 strings
JavaScript
11
star
24

go-screeps

Go
10
star
25

ScreepsOS

Screeps/Serializable Operation System (SOS)
JavaScript
9
star
26

screeps-regenerator

Use generators in Screeps
JavaScript
8
star
27

screeps-backup

Backups for Screeps
Python
7
star
28

screeps-tools

Tools to help players of the Programming MMO Screeps
TypeScript
7
star
29

ScreepsPerformanceServer

Ready to run server with performance checks
JavaScript
7
star
30

Screeps-Script-Downloader

Download all your Screeps scripts without the need to copy-paste each individual file.
Java
6
star
31

screeps-drawing

JavaScript
2
star
32

screeps-closure-compiler-externs

Screeps externs for use with Closure Compiler.
JavaScript
2
star
33

screeps-server-test

JavaScript
2
star
34

screeps-arena-javascript-starter

JavaScript
2
star
35

ErrorExporter

An error exporter instead of email
JavaScript
2
star
36

snippets-logging

Long term safe keeping for snippets from the #logging slack channel
JavaScript
2
star
37

screeps-jconsole

Node based console for Screeps MMORTS based on screeps-console by stargateur
JavaScript
1
star
38

ScreepsDiplomacyExtensions

A universal verb and framing protocol built on ScreepsDiplomacy
1
star
39

screeps_slack_bot

Slack Bot for providing information about the Screeps world
JavaScript
1
star
40

ScreepsUserTracker

JavaScript
1
star
41

screeps-stats-lib

A client-side library to enhance the output of screeps-stats statistics.
JavaScript
1
star
42

simpleAllies

Allows for simple communication between allies using a public segment
TypeScript
1
star