Skip to content

Cannot import npm package with definition defined in typeroots #11137

Closed
@jonaskello

Description

@jonaskello

TypeScript Version: 2.0.3

I am trying to make a definition file for an existing npm package without using ambient definition such as declare module 'foo' {...}. In the example below I am trying to create typings for the npm package fibonacci.

Code

package.json:

{
  "name": "typings_test",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "fibonacci": "^1.6.1"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "js",
    "typeRoots": ["./types"]
  },
  "exclude": [
    "node_modules"
  ]
}

test.ts:

import * as fibonacci from 'fibonacci';

const bigNumber = fibonacci.iterate(3000);
console.log (bigNumber);

types/fibonacci/index.d.ts:

export function iterate(x:number);

Compile to see the error:

$ tsc
test.ts(1,28): error TS2307: Cannot find module 'fibonacci'

Expected behavior:

My expectation would be that tsc should pick up the definitions in types/fibonacci/index.d.ts because of "typeRoots": ["./types"] in tsconfig.json.

Actual behavior:

I get error TS2307: Cannot find module 'fibonacci'.. Using --traceResolution I can see that tsc is finding the file types/fibonacci/index.d.ts. If I move the file types/fibonacci/index.d.ts into node_modules/fibonacci/index.d.ts it works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptWorking as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions