screeps-typescript-starter icon indicating copy to clipboard operation
screeps-typescript-starter copied to clipboard

Can't access role from CreeperMemory

Open JQE opened this issue 5 years ago • 8 comments
trafficstars

I setup the starter kit, and it works, then i start to add code.

I am now using the simplest thing i can think of

for (var name in Game.creeps) { if (Game.creeps[name].memory.role == "Harvester") { console.log("Harvester"); } }

When running this through the tester (or in game) i get the following error `src/main.ts:15:34 - error TS2339: Property 'role' does not exist on type 'CreepMemory'.

15 if (Game.creeps[name].memory.role == "Harvester") {`

If i remove the interface definition from @types/screeps then it works. It seems that for some reason i can't merge the types using the types.d.ts.

I am new to typescript definitions, anyone got any advice?

JQE avatar Sep 30 '20 19:09 JQE

You have to update the types.d.ts file to include the role, which will be a string. You'll also need to use === per ESLint.

Best regards, Derek

Derek R. Austin, PT, DPT, MS, BCTMB, LMT, CSCS

Read my blog on Medium: https://medium.com/@DoctorDerek

Join me on LinkedIn: https://www.linkedin.com/in/derek-austin/

On Wed, Sep 30, 2020, 2:03 PM JQE [email protected] wrote:

I setup the starter kit, and it works, then i start to add code.

I am now using the simplest thing i can think of

for (var name in Game.creeps) { if (Game.creeps[name].memory.role == "Harvester") { console.log("Harvester"); } }

When running this through the tester (or in game) i get the following error `src/main.ts:15:34 - error TS2339: Property 'role' does not exist on type 'CreepMemory'.

15 if (Game.creeps[name].memory.role == "Harvester") {`

If i remove the interface definition from @types/screeps then it works. It seems that for some reason i can't merge the types using the types.d.ts.

I am new to typescript definitions, anyone got any advice?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/screepers/screeps-typescript-starter/issues/146, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMV42QXC4EKFPYLWRGWZYNLSIN6HVANCNFSM4R7O4KUQ .

djD-REK avatar Oct 01 '20 21:10 djD-REK

It is in the types.d.the by default.

The === is irrelevant as it won’t compile as it doesn’t see the role. Removing the interface definition from the screens types fixes it.

Sent from my iPhone

On Oct 1, 2020, at 4:50 PM, Dr. Derek Austin [email protected] wrote:



You have to update the types.d.ts file to include the role, which will be a string. You'll also need to use === per ESLint.

Best regards, Derek

Derek R. Austin, PT, DPT, MS, BCTMB, LMT, CSCS

Read my blog on Medium: https://medium.com/@DoctorDerek

Join me on LinkedIn: https://www.linkedin.com/in/derek-austin/

On Wed, Sep 30, 2020, 2:03 PM JQE [email protected] wrote:

I setup the starter kit, and it works, then i start to add code.

I am now using the simplest thing i can think of

for (var name in Game.creeps) { if (Game.creeps[name].memory.role == "Harvester") { console.log("Harvester"); } }

When running this through the tester (or in game) i get the following error `src/main.ts:15:34 - error TS2339: Property 'role' does not exist on type 'CreepMemory'.

15 if (Game.creeps[name].memory.role == "Harvester") {`

If i remove the interface definition from @types/screeps then it works. It seems that for some reason i can't merge the types using the types.d.ts.

I am new to typescript definitions, anyone got any advice?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/screepers/screeps-typescript-starter/issues/146, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMV42QXC4EKFPYLWRGWZYNLSIN6HVANCNFSM4R7O4KUQ .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/screepers/screeps-typescript-starter/issues/146#issuecomment-702416713, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAHT7ENRFIURXUOVOH5WSYDSIT2S7ANCNFSM4R7O4KUQ.

JQE avatar Oct 01 '20 21:10 JQE

Interfaces are merged by the TS processor.

Effectively,

  -- @types/screeps
  interface CreepMemory {}

and

  -- types.d.ts
  interface CreepMemory {
    role: string;
  }

should become merged into one definition for CreepMemory.

Something that could be breaking this would be using import or export in types.d.ts.

I just did a fresh clone from the repo and pasted your initial for loop into src/main.ts and it compiles correctly for me.

What is the content of your types.d.ts file? What version of @types/screeps do you have in package.json?

pyrodogg avatar Oct 02 '20 05:10 pyrodogg

OK. Node version v10.19.0 npm version 6.14.4 yarn version 1.22.5 Commands

git clone https://github.com/screepers/screeps-typescript-starter.git
cd screeps-typescript-starter
yarn install
code src/main.ts

add

for (var name in Game.creeps) {
    if (Game.creeps[name].memory.role === "Harvester") {
      console.log("Harvester")
    }
  }
yarn test
src/main.ts:16:34 - error TS2339: Property 'role' does not exist on type 'CreepMemory'.

16     if (Game.creeps[name].memory.role === "Harvester") {

contents of types.d.ts

// example declaration file - remove these and add your own custom typings

// memory extension samples
interface CreepMemory {
  role: string;
  room: string;
  working: boolean;
}

interface Memory {
  uuid: number;
  log: any;
}

// `global` extension samples
declare namespace NodeJS {
  interface Global {
    log: any;
  }
}

contents of package.json

{
  "name": "screeps-typescript-starter",
  "version": "3.0.0",
  "description": "",
  "main": "index.js",
  "//": "If you add or change the names of destinations in screeps.json, make sure you update these scripts to reflect the changes",
  "scripts": {
    "lint": "eslint \"src/**/*.ts\"",
    "build": "rollup -c",
    "push-main": "rollup -c --environment DEST:main",
    "push-pserver": "rollup -c --environment DEST:pserver",
    "push-sim": "rollup -c --environment DEST:sim",
    "test": "npm run test-unit",
    "test-unit": "mocha test/unit/**/*.ts",
    "test-integration": "echo 'See docs/in-depth/testing.md for instructions on enabling integration tests'",
    "watch-main": "rollup -cw --environment DEST:main",
    "watch-pserver": "rollup -cw --environment DEST:pserver",
    "watch-sim": "rollup -cw --environment DEST:sim"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/screepers/screeps-typescript-starter.git"
  },
  "author": "",
  "license": "Unlicense",
  "bugs": {
    "url": "https://github.com/screepers/screeps-typescript-starter/issues"
  },
  "homepage": "https://github.com/screepers/screeps-typescript-starter#readme",
  "engines": {
    "node": "10.x || 12.x"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^11.1.0",
    "@rollup/plugin-node-resolve": "^7.1.3",
    "@types/chai": "^4.1.6",
    "@types/lodash": "3.10.2",
    "@types/mocha": "^5.2.5",
    "@types/node": "^13.13.1",
    "@types/screeps": "^3.1.0",
    "@types/sinon": "^5.0.5",
    "@types/sinon-chai": "^3.2.0",
    "@typescript-eslint/eslint-plugin": "^3.7.0",
    "@typescript-eslint/parser": "^3.7.0",
    "@typescript-eslint/typescript-estree": "^3.7.0",
    "chai": "^4.2.0",
    "eslint": "^6.8.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-import-resolver-typescript": "^2.0.0",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-prettier": "^3.1.4",
    "lodash": "^3.10.1",
    "mocha": "^5.2.0",
    "prettier": "^2.0.4",
    "rollup": "^2.6.1",
    "rollup-plugin-clear": "^2.0.7",
    "rollup-plugin-screeps": "^1.0.0",
    "rollup-plugin-typescript2": "^0.27.0",
    "sinon": "^6.3.5",
    "sinon-chai": "^3.2.0",
    "ts-node": "^8.8.2",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.8.3"
  },
  "dependencies": {
    "source-map": "~0.6.1"
  }
}

JQE avatar Oct 02 '20 11:10 JQE

Thanks for the additional detail. I didn't fully understand that you were having troubles with running the test script. I had only tested the build script, which worked.

I've been able to reproduce the error with npm test and yarn test. Running npm run build and yarn run build both work.

I suspect then this might be related to the recent changes merging #144 . I'll continue to investigate. In the meantime, you should be able to run the build script.

pyrodogg avatar Oct 02 '20 17:10 pyrodogg

The build script does complete. However it doesn’t run on the server. Same error.

Sent from my iPhone

On Oct 2, 2020, at 12:46 PM, Skyler Kehren [email protected] wrote:



Thanks for the additional detail. I didn't fully understand that you were having troubles with running the test script. I had only tested the build script, which worked.

I've been able to reproduce the error with npm test and yarn test. Running npm run build and yarn run build both work.

I suspect then this might be related to the recent changes merging #144https://github.com/screepers/screeps-typescript-starter/pull/144 . I'll continue to investigate. In the meantime, you should be able to run the build script.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/screepers/screeps-typescript-starter/issues/146#issuecomment-702869891, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAHT7ELTLYVAM4ENFB7NE7DSIYGWTANCNFSM4R7O4KUQ.

JQE avatar Oct 02 '20 17:10 JQE

Well, by the time the script is running on the server, it is plain JS and types have nothing to do with it. If you're getting a runtime error because a role property doesn't exist in the creeps memory it's because you've never set it.

All of the CreepMemory examples are just that, examples of how to define your own typed memory properties. None of them are built into the game.

pyrodogg avatar Oct 02 '20 18:10 pyrodogg

It turns out you are correct. I thought I had tried running the code on the server, however it would appear i did not. It runs properly. Just the tests fail.

JQE avatar Oct 02 '20 18:10 JQE