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

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

Open
Caedendi opened this issue Nov 1, 2024 · 5 comments

Comments

@Caedendi
Copy link

Caedendi commented Nov 1, 2024

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 Caedendi changed the title [TypeScript] can't reference D2RMM types in mod because definitions are outside of the /mods/ folder [TypeScript] can't reference D2RMM's own types in mods because the definitions are outside of the /mods/ folder Nov 1, 2024
@olegbl
Copy link
Owner

olegbl commented Nov 2, 2024

So, the way types.d.ts works is a bit magical in that any TS environment should automatically import the nearest types.d.ts up the directory structure without you having to specify it. So, you shouldn't ever need to import it manually like this.

@olegbl olegbl closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2024
@Caedendi
Copy link
Author

Caedendi commented Nov 2, 2024

Well I guess that seems to work, but it still leaves me with my IDE showing a lot of errors because it can't find those types. Is there a way to fix that?

@olegbl
Copy link
Owner

olegbl commented Nov 3, 2024

Oh, I think I understand your question - you want to reference the types in types.d.ts (such as JSONData) directly rather than just having D2RMM APIs be strongly typed. I think I'd just need to declare those types globally in types.d.ts.

@olegbl olegbl reopened this Nov 3, 2024
@Caedendi
Copy link
Author

Caedendi commented Nov 3, 2024

Exactly, that's what I meant. For this line right here:

protected findJsonFileEntryByKey(jsonFile: JSONData, key: string): Record<string, string> { // PROBLEM HERE

So I can define param jsonFile to be of type JSONData without getting errors.

@olegbl
Copy link
Owner

olegbl commented Nov 29, 2024

BTW, until the exporting is done, you can always derive these types. e.g.:

type MethodReturn<TMethod> = TMethod extends (
  ...args: infer TArgs
) => infer TReturn
  ? TReturn
  : never;

type JSONData = MethodReturn<typeof D2RMM.readJson>;

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

2 participants