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

MISC: Change how enums are exposed in NetscriptDefinitions.d.ts #1998

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

catloversg
Copy link
Contributor

We expose enums with declare enum for typing (technically, the reason is more complicated, but let's simplify it here) and ns.enums for runtime data. Some players assume that they can access those enums at runtime by using those declared enums, but those enums do not exist at runtime like they think. For example: #1198. We can resolve this problem by changing how we expose enums to players in that file.

At first, there are tons of discussions about TS enum and its alternatives on the Discord server and everywhere on the Internet. At least, please read ⁠Enums and some old discussions on the Discord server (search "enum" and check "Old").

Scope of this change:

  • Only touch declare enum in NetscriptDefinitions.d.ts.
  • No corporation or other stuff.
  • Minimal change or no change in internal code.

Things to remember:

  • ns.enums.Something must have proper types and work as normal.
  • Shoud not need the old trick LocationName | ${LocationName}.
  • Must be able to be extracted by api-extractor.
export async function main(ns: NS) {
  // Must be ok and support autocomplete
  ns.singularity.travelToCity("Sector-12");
  ns.singularity.travelToCity(ns.enums.CityName.Sector12);

  // Must be ok
  const city1: CityName = ns.enums.CityName.Sector12;
  ns.singularity.travelToCity(city1);

  // Must be ok
  const city2: CityName = "Sector-12";
  ns.singularity.travelToCity(city2);

  // Must be an error
  // const city3: CityName = "Sector-13";
  // console.log(city3);

  // Hover "city". It must show `const city: "Aevum" | "Chongqing" | "Ishima" | "Volhaven" | "Sector-12" | "New Tokyo"`
  for (const city of Object.values(ns.enums.CityName)) {
    // Must be ok
    ns.singularity.travelToCity(city);
  }
}

This PR won't touch internal enums. Changing all internal enums to as const objects is a bag of worms. It needs to be done in another PR.

This PR is in the draft state now. After we agree on how to do it, I'll expand the change to other enums and run npm run doc.

@catloversg catloversg marked this pull request as draft March 6, 2025 04:12
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

Successfully merging this pull request may close these issues.

2 participants