Skip to content

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

Closed

Description

@ikatyang

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 [email protected], 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.

Activity

  1. mhegazy commented on Mar 2, 2017

    @mhegazy
    Contributor

    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

  2. added
    Duplicate
    on Mar 2, 2017
  3. dhruvdutt commented on Jun 2, 2017

    @dhruvdutt

    Edit your tsconfig.json

    "noImplicitAny": false

  4. ikatyang commented on Jun 2, 2017

    @ikatyang
    ContributorAuthor

    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.

  5. dhruvdutt commented on Jun 2, 2017

    @dhruvdutt

    Ah! Alrighty. 👍

  6. TrungRueta commented on Dec 24, 2017

    @TrungRueta

    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!

  7. bennycode commented on Feb 19, 2018

    @bennycode
    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".

  8. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions