gamedig / node-gamedig

Query game servers and not only! Node.JS/Deno/Bun or Bash (via the CLI).
https://www.npmjs.com/package/gamedig
MIT License
606 stars 148 forks source link

bug: TypeScript types not working for package types `module` #569

Closed FlorianSW closed 3 months ago

FlorianSW commented 4 months ago

Describe the bug Using the latest gamedig and @types/gamedig packages in my project, I get the following TypeScript compile error:

error TS7016: Could not find a declaration file for module 'gamedig'. '<...>/workspace/discord-player-count-bot/node_modules/gamedig/lib/index.js' implicitly has an 'any' type.
  There are types at '<...>/workspace/discord-player-count-bot/node_modules/@types/gamedig/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@types/gamedig' library may need to update its package.json or typings.

3 import {GameDig, QueryOptions} from "gamedig";

This is my package.json

{
  "name": "discord-player-count",
  "version": "1.0.0",
  "exports": "./dist/index.js",
  "scripts": {
    "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
    "build": "del-cli dist && tsc",
    "prestart": "npm run build",
    "start": "node ./dist/index.js"
  },
  "devDependencies": {
    "@types/gamedig": "^5.0.2",
    "@types/jest": "^29.2.1",
    "@types/node": "^16.18.3",
    "del-cli": "^5.0.0",
    "jest": "^29.2.2",
    "ts-jest": "^29.0.3",
    "typescript": "^5.4.5"
  },
  "jest": {
    "testEnvironment": "node",
    "extensionsToTreatAsEsm": [".ts"],
    "modulePathIgnorePatterns": [
      "<rootDir>/dist/"
    ],
    "moduleNameMapper": {
      "^(\\.{1,2}/.*)\\.js$": "$1"
    },
    "transform": {
      "^.+\\.tsx?$": [
        "ts-jest",
        {
          "useESM": true
        }
      ]
    }
  },
  "engines": {
    "node": "20.x.x"
  },
  "optionalDependencies": {
    "fsevents": "*"
  },
  "type": "module",
  "author": "Florian Schmidt",
  "license": "MIT",
  "dependencies": {
    "cftools-sdk": "^3.1.7",
    "discord.js": "^14.6.0",
    "dotenv": "^16.0.3",
    "gamedig": "^5.0.0",
    "got": "^12.5.2",
    "rxjs": "^7.5.7"
  }
}

And my tsconfig.json:

{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "src",
    "lib": ["ES2020", "dom"],
    "moduleResolution": "node16",
    "moduleDetection": "force",

    "target": "ES2020",                       
    "module": "node16",                       

    "strict": true,                           

    "allowSyntheticDefaultImports": true,

    "skipLibCheck": true,                     
    "forceConsistentCasingInFileNames": true  
  }
}

When switching to commonjs, typings work fine, however, this is not really an option for me here. Tbh, I don't have a direct clue on what is wrong here, is it a configuration error on my side? Or are the types not working with modules?

a-sync commented 3 months ago

I have an ESM project using the gamedig types and it builds fine atm (node 20 / TS 5) :shipit:
The tsconfig:

{
    "compilerOptions": {
        "baseUrl": ".",
        "rootDir": "./src",
        "outDir": "./dist",
        "lib": [
            "es2020"
        ],
        "module": "esnext",
        "target": "es2019",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true,
        "moduleResolution": "node"
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}

Here is the source for the package.json file the error refers to: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/gamedig/package.json
but you should check the referenced _'<...>/workspace/discord-player-count-bot/nodemodules/@types/gamedig/index.d.ts' file maybe. Other then trying to delete the node_modules + package-lock and reinstalling. 🤷‍♂️

Update: according to this issue: https://github.com/microsoft/TypeScript/issues/52363
moduleResolution: node16 is the problem and the types need some fixing: https://arethetypeswrong.github.io/?p=gamedig%405.0.0
will take a look at this over the weekend.

a-sync commented 3 months ago

The fix is editing the imported modules package json to show:

"exports": {
    ".": {
        "import": {
            "types": "./index.d.ts",
            "default": "./index.js"
        },
        "require": {
            "types": "./index.d.cts",
            "default": "./index.cjs"
        }
    }
},

After that it lets me build with "moduleResolution": "node16", while whithout it i get the same error.

@FlorianSW i submitted a PR to definetly typed, hopefully this gets added soon, then you should be good to go. https://github.com/DefinitelyTyped/DefinitelyTyped/pull/69767

a-sync commented 3 months ago

Updates are merged upstream, so this should be fixed now :shipit:

FlorianSW commented 3 months ago

Updates are merged upstream, so this should be fixed now :shipit:

Awesome :) I just tested it and now the types are recognized and uses, just as one would expect. Big thanks for your fast update :)

CosminPerRam commented 3 months ago

Kudos for @a-sync for maintaining the type bindings (: