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: TS7016 Could not find a declaration file for module #14379

Closed
ikatyang opened this issue Mar 1, 2017 · 6 comments
Closed

bug: TS7016 Could not find a declaration file for module #14379

ikatyang opened this issue Mar 1, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@ikatyang
Copy link
Contributor

ikatyang commented Mar 1, 2017

TypeScript Version: 2.2.1 / nightly (2.3.0-dev.20170301)

Command

tsc try.ts --noImplicitAny

Code

try.ts

import { x } from 'test'; // error TS7016: Could not find a declaration file for module 'test'.

module test

  • package.json

    {
      "name": "test",
      "version": "1.0.0",
      "main": "lib/index"
    }
  • lib/index.js

    "use strict";
    exports.__esModule = true;
    exports.x = { test: "only" };
  • lib/index.d.ts

    export interface X {
      test: "only";
    }
    export declare const x: X;

Expected behavior:

non-error

Actual behavior:

try.ts(1,19): error TS7016: Could not find a declaration file for module 'test'. '~/tsc-test/node_modules/test/lib/index.js' implicitly has an 'any' type.

It works fine with typescript@2.1.6, or specified types field in package.json:

{
  "name": "test",
  "version": "1.0.0",
  "main": "lib/index",
  "types": "lib/index"
}

But I think this is a bug, since the documentation said:

Also note that if your main declaration file is named index.d.ts and lives at the root of the package (next to index.js) you do not need to mark the "types" property, though it is advisable to do so.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 2, 2017

The behavior matches the documentation. the index.d.ts is not at the root, it is under lib.

The request to look up <value of package.json::main>.d.ts is tracked for #9854

@dhruvdutt
Copy link

Edit your tsconfig.json

"noImplicitAny": false

@ikatyang
Copy link
Contributor Author

ikatyang commented Jun 2, 2017

It was not about how to hide the error message but for what strategy does TS use to find declaration files.

I used to think that the "root" means "main" field (./lib/index.js in this case) in package.json, so that I just have to place ./lib/index.d.ts next to ./lib/index.js.

The fact is that the "root" means where package.json stands.

@dhruvdutt
Copy link

Ah! Alrighty. 👍

@TrungRueta
Copy link

TrungRueta commented Dec 24, 2017

I know this question is old. But i got same issue and found way to resolve:
from tsconfig.json of module you made. add :

"typeRoots": [
     "lib"
 ],

with "lib" is folder "outDir" value. Assume you put .d.ts files same place with .js file. and it will be good!

@bennycode
Copy link
Contributor

I used to think that the "root" means "main" field (./lib/index.js in this case) in package.json, so that I just have to place ./lib/index.d.ts next to ./lib/index.js.

I agree that the documentation is quite misleading. It should say "next to package.json" instead of "next to index.js".

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants