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

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

Open
FlorianSW opened this issue May 29, 2024 · 3 comments
Open

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

FlorianSW opened this issue May 29, 2024 · 3 comments
Labels

Comments

@FlorianSW
Copy link

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?

@FlorianSW FlorianSW added the bug label May 29, 2024
@a-sync
Copy link
Contributor

a-sync commented Jun 4, 2024

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/node_modules/@types/gamedig/index.d.ts' file maybe. Other then trying to delete the node_modules + package-lock and reinstalling. 🤷‍♂️

Update:
according to this issue: microsoft/TypeScript#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
Copy link
Contributor

a-sync commented Jun 9, 2024

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. DefinitelyTyped/DefinitelyTyped#69767

@a-sync
Copy link
Contributor

a-sync commented Jun 27, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants