d2rmm icon indicating copy to clipboard operation
d2rmm copied to clipboard

[TypeScript] can't reference D2RMM's own types in mods because the definitions are outside of the /mods/ folder

Open Caedendi opened this issue 4 months ago • 5 comments

TypeScript support has been a blessing for my modding. One issue that I've run into a few times though is that D2RMM doesn't support its own types.

To reproduce, add this file to a mod and try to install it.

(Don't mind the technical side of what the code is supposed to do, it's still a work in progress)

import { JSONData } from "../../types"; // PROBLEM HERE

export class GameDataTest {
  protected skillsJsonFile: JSONData;

  constructor() {
    this.loadFiles();
    let row = this.findJsonFileEntryByKey(this.skillsJsonFile, "skillname59"); // blizzard
  }

  protected loadFiles(): void {
    this.skillsJsonFile = D2RMM.readJson(`global\\excel\\skills.json`);
  }


  protected findJsonFileEntryByKey(jsonFile: JSONData, key: string): Record<string, string> { // PROBLEM HERE
    let match = Object.entries(jsonFile).find(([_, value]) => value["key"] === key);
    return match ? { [match[0]]: match[1]["key"] } : {};
  }
}

D2RMM will give this error message:

[MOD NAME] Mod encountered a compile error!
Error: BridgeAPI.readModCode: Failed to compile mod: Error: BridgeAPI.readModCode: Failed to read mods\MODFOLDER\../../types.ts
    at I (C:\Users\Caedendi\Games\D2RMM\resources\app.asar\dist\main\worker.js:2:163626)
    at Object.readModCode (C:\Users\Caedendi\Games\D2RMM\resources\app.asar\dist\main\worker.js:2:174289)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async installMods (C:\Users\Caedendi\Games\D2RMM\resources\app.asar\dist\main\worker.js:2:176643)

Caedendi avatar Nov 01 '24 18:11 Caedendi