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

Could not find a declaration file for module 'ava'. 'node_modules/ava/entrypoints/main.mjs' implicitly has an 'any' type. #2934

Closed
linaGirl opened this issue Jan 6, 2022 · 5 comments

Comments

@linaGirl
Copy link

linaGirl commented Jan 6, 2022

My test code is written in TypeScript but the type declarations cannot be found when using ava version 4. With ava version 3 it seems to work. This is probably due to the missing index.js file in the ava package?

Test file: tests/my.test.ts

import test from 'ava';

run:

node ./node_modules/.bin/tsc

output:

tests/my.test.ts:1:18 - error TS7016: Could not find a declaration file for module 'ava'. 'node_modules/ava/entrypoints/main.mjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/ava` if it exists or add a new declaration (.d.ts) file containing `declare module 'ava';`

1 import test from 'ava';
                   ~~~~~


Found 1 error in tests/my.test.ts:1

package.json

{
  "type": "module",
  "name": "test",
  "version": "1.0.0",
  "devDependencies": {
    "@ava/typescript": "^3.0.1",
    "@tsconfig/node16": "^1.0.2",
    "@types/node": "^17.0.5",
    "ava": "^4.0.0",
    "eslint": "^8.6.0",
    "typescript": "^4.6.0-dev.20220105"
  },
  "scripts": {
    "build": "node ./node_modules/.bin/tsc",
    "test": "node ./node_modules/.bin/tsc && ava"
  }
}

ava.config.js

export default {
    "typescript": {
        "rewritePaths": {
            "tests/": "build/tests/"
        },
        "compile": false
    }
}

tsconfig.json

{
  "extends": "@tsconfig/node16/tsconfig.json",
  "compilerOptions": {
    "preserveConstEnums": true,
    "outDir": "build",
    "strictPropertyInitialization": false,
    "target": "es2021",
    "module": "nodenext",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "sourceMap": true,
    "declaration": true
  },
  "include": ["tests/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

Maybe i'm missing something. I'm happy to provide a repo for reproducing the problem.

@novemberborn
Copy link
Member

What happens with a stable TypeScript version, rather than a pre-release?

@linaGirl
Copy link
Author

linaGirl commented Jan 6, 2022

Thanks @novemberborn it works with the stable version

@novemberborn
Copy link
Member

Fingers crossed then it's a TS bug that'll go away before final release.

@t2t2
Copy link

t2t2 commented May 25, 2022

With typescript 4.7 release seems like it's still an issue when the project has "type": "module" and tsconfig's "module": "NodeNext" (well, and strict mode, otherwise it will just warn and doesn't give any autocomplete info)

  Uncaught exception in tests\sample.test.ts

  tests/sample.test.ts(1,18): error TS7016: Could not find a declaration file for module 'ava'. '.../ava-ts-module/node_modules/ava/entrypoints/main.cjs' implicitly has an 'any' type.    Try `npm i --save-dev @types/ava` if it exists or add a new declaration (.d.ts) file containing `declare module 'ava';`
  tests/sample.test.ts(3,18): error TS7006: Parameter 't' implicitly has an 'any' type.

minimal repo:

package.json:

{
  "name": "ava-ts-module",
  "type": "module",
  "version": "1.0.0",
  "scripts": {
    "test": "ava"
  },
  "devDependencies": {
    "ava": "^4.2.0",
    "ts-node": "^10.8.0",
    "typescript": "^4.7.2"
  },
  "ava": {
    "extensions": {
      "ts": "module"
    },
    "nodeArguments": [
      "--loader=ts-node/esm"
    ]
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "NodeNext",
    "strict": true
  }
}

tests\sample.test.ts

import test from 'ava';

test('one test', t => {
	t.pass();
});

Seems like adding "types": "./index.d.ts" to exports map is makes it work, but probably also needs types for other exports

@novemberborn
Copy link
Member

@t2t2 see #3024.

@avajs avajs locked and limited conversation to collaborators May 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants