Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access role from CreeperMemory #146

Open
JQE opened this issue Sep 30, 2020 · 8 comments
Open

Can't access role from CreeperMemory #146

JQE opened this issue Sep 30, 2020 · 8 comments

Comments

@JQE
Copy link

JQE commented Sep 30, 2020

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?

@djD-REK
Copy link
Contributor

djD-REK commented Oct 1, 2020 via email

@JQE
Copy link
Author

JQE commented Oct 1, 2020 via email

@pyrodogg
Copy link
Member

pyrodogg commented Oct 2, 2020

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?

@JQE
Copy link
Author

JQE commented Oct 2, 2020

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"
  }
}

@pyrodogg
Copy link
Member

pyrodogg commented Oct 2, 2020

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.

@JQE
Copy link
Author

JQE commented Oct 2, 2020 via email

@pyrodogg
Copy link
Member

pyrodogg commented Oct 2, 2020

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.

@JQE
Copy link
Author

JQE commented Oct 2, 2020

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.

@pyrodogg pyrodogg mentioned this issue Oct 2, 2020
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants