question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot resolve .d.ts file -- use `import type` or triple-slash reference instead

See original GitHub issue

What happens and why it is wrong

When compiling a TS file via rollup and the typescript 2 plugin, the .d.ts from which it imports a few things cannot be found.

I have a single Typescript file that I wish to build to a UMD module via rollup. There’s a dedicated typescript config file for it, and the rollup config contains 3 instances of this setup (the rollup config given below being just for this one that fails). The directory structure is:

- rollup.config.js
- ts-config
    - ts-config-io.json
- ts
    - io.ts

I run this command from the directory in which rollup.config.js resides:

rollup --config

The top of io.ts looks like this:

import { AClass } from "../types/something-sensitive/index";

I’ve pasted the verbose output below, but the crux of the matter is this error:

Could not resolve '../types/something-sensitive/index' from ts/io.ts

Am I sure this should work?

idk…with the plugin and rollup? i’m new to the world of rollup. perhaps i missed something. However, if i run this command from the same directory in which rollup.config.js resides:

tsc -p ts-config/tsconfig-io.json

The typescript file is successfully built to JS (with the output going to ts-config\_build\io.js, if that helps).

So have I done something wrong here? Missed a setting or something? I can’t tell if rollup is trying to resolve the .d.ts at all, or if it is and it’s just in some other working directory etc. Another of the TS files I build in this same rollup config file, with the exact same rollup settings, does not import any typings and everything works as expected - the JS is built and rollup, er, rolls it up.

Any suggestions you could make would be appreciated.

Environment

i’m on a mac; i doubt this has anything to do with the situation.

Versions

  • typescript: 2.6.2
  • rollup: 0.58.2
  • rollup-plugin-typescript2: 0.13.0

rollup.config.js

{
        input: 'ts/io.ts',
        output: [{
            name: 'SomeIOThing',
            file: '_build/io.js',
            format: 'umd'
        }],
        plugins: [
            typescript({

                check: true,
                verbosity: 3,
                clean: true,
                rollupCommonJSResolveHack: false,
                typescript: require("typescript"),

                tsconfig: "./ts-config/tsconfig-io.json",
                tsconfigOverride: {
                    compilerOptions: { }
                }
            })
        ]
    }

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "outDir": "_build",
    "moduleResolution": "node",
    "sourceMap": true,
    "declaration":true,
    "emitDecoratorMetadata": false,
    "experimentalDecorators": false,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true

  },
  "files":[ "../ts/io.ts" ]
}

package.json

"rollup": "^0.58.2",
    "rollup-plugin-node-resolve": "^3.3.0",
    "rollup-plugin-typescript2": "^0.13.0",

(although note that the node resolve plugin is unused)

plugin output with verbosity 3

ts/io.ts    "clean": true,
 → _build/io.js    "rollupCommonJSResolveHack": false,
...    "typescript": "version 2.6.2",

    "tsconfig": "./ts-config/tsconfig-io.json",
    "tsconfigOverride": {
        "compilerOptions": {}
    },
    "cacheRoot": "/something-sensitive/.rpt2_cache",
    "include": [
        "*.ts+(|x)",
        "**/*.ts+(|x)"
    ],
    "exclude": [
        "*.d.ts",
        "**/*.d.ts"
    ],
    "abortOnError": true,
    "useTsconfigDeclarationDir": false,
    "tsconfigDefaults": {}
}
rpt2: rollup config:
{
    "external": [
        "",
        ""
    ],
    "input": "ts/io.ts",
    "chunkGroupingSize": 5000,
    "perf": false,
    "plugins": [
        {
            "name": "rpt2"
        }
    ],
    "entry": "ts/io.ts"
}
rpt2: built-in options overrides: {
    "noEmitHelpers": false,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "outDir": "/something-sensitive",
    "moduleResolution": 2,
    "declarationDir": "/something-sensitive"
}
rpt2: parsed tsconfig: {
    "options": {
        "module": 5,
        "target": 1,
        "outDir": "/something-sensitive",
        "moduleResolution": 2,
        "sourceMap": true,
        "declaration": true,
        "emitDecoratorMetadata": false,
        "experimentalDecorators": false,
        "lib": [
            "lib.es2015.d.ts",
            "lib.dom.d.ts"
        ],
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true,
        "noEmitHelpers": false,
        "importHelpers": true,
        "noResolve": false,
        "noEmit": false,
        "declarationDir": "/something-sensitive",
        "configFilePath": "/something-sensitive/./ts-config/tsconfig-io.json"
    },
    "fileNames": [
        "/something-sensitive/ts/io.ts"
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "compilerOptions": {
            "module": "es2015",
            "target": "es5",
            "outDir": "_build",
            "moduleResolution": "node",
            "sourceMap": true,
            "declaration": true,
            "emitDecoratorMetadata": false,
            "experimentalDecorators": false,
            "lib": [
                "es2015",
                "dom"
            ],
            "noImplicitAny": true,
            "suppressImplicitAnyIndexErrors": true
        },
        "files": [
            "../ts/io.ts"
        ]
    },
    "errors": [],
    "wildcardDirectories": {},
    "compileOnSave": false,
    "configFileSpecs": {
        "filesSpecs": [
            "../ts/io.ts"
        ],
        "excludeSpecs": [
            "_build"
        ],
        "validatedExcludeSpecs": [
            "_build"
        ],
        "wildcardDirectories": {}
    }
}
rpt2: included:
'[
    "*.ts+(|x)",
    "**/*.ts+(|x)"
]'
rpt2: excluded:
'[
    "*.d.ts",
    "**/*.d.ts"
]'
rpt2: Ambient types:
rpt2:     /something-sensitive/node_modules/@types/estree/index.d.ts
rpt2:     /something-sensitive/node_modules/@types/node/index.d.ts
rpt2: ambient types changed, redoing all semantic diagnostics
rpt2: transpiling '/something-sensitive/ts/io.ts'
rpt2:     cache: '491e0fb3838a963f725831db27ee532c020c6ebf'
rpt2:     cache miss
rpt2:     cache: '491e0fb3838a963f725831db27ee532c020c6ebf'
rpt2:     cache miss
rpt2:     cache: '491e0fb3838a963f725831db27ee532c020c6ebf'
rpt2:     cache miss
rpt2: generated declarations for '/something-sensitive/ts/io.ts'
[!] Error: Could not resolve '../types/something-sensitive/index' from ts/io.ts

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jayportacommented, Jun 13, 2022

Sorry, I know this is closed but, I had the same problem and stumbled upon a post in tsdx’s issues (however I’m not using tsdx) that adding type to the import statement works, so I tried it and… it works. Rollup builds successfully.

In my project’s index.ts: import type MyType from './types/index.d

index.d looks something like this:

export * from './typesA.d'
export * from './typesB.d'
export * from './typesC.d'
export * from './typesD.d'

My goal was just to organize types in files without creating an object with nested types.

Example:

I wanted this: MyTypes.Something not MyTypes.TypesA.Something

I hope this helps someone like me who searched for a while and took a while to find a solution.

2reactions
RexSkzcommented, Oct 10, 2019

Hi, I encountered the same problem just now when using a third-party library which has no typeings, and found a solution.

In TypeScript, we shouldn’t use “import” syntax to import .d.ts file, instead we should use “triple-slash directives” provided by TypeScript like this:

/// <reference path="./path/to/my-custom-declares.d.ts" />
import Something from 'lib-without-types';

Now both my editor and tsc produce no error output.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Triple-Slash Directives
A triple-slash reference path is resolved relative to the containing file, ... a .ts file, use types on the command line or in...
Read more >
node.js - How to use TypeScript import statement instead of ...
d.ts, use import instead. OK, I am going to use export/import later anyway, so I've edited the triple slash reference ...
Read more >
Using TypeScript | Manual
Instead, imported modules are specified as files (including extensions) or fully ... Why not use the triple-slash type reference in TypeScript files?
Read more >
Features
Vite supports importing .ts files out of the box. Vite only performs transpilation on .ts files and does NOT perform type checking. It...
Read more >
Relative vs. Non-relative module imports
Module resolution is the process the compiler uses to figure out what an import ... A relative import is resolved relative to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found