[email protected] ignore local type declaration files

Created on 2 Jul 2018  Â·  16Comments  Â·  Source: TypeStrong/ts-node

Sometimes we need to declare module for packages without type declaration.
And [email protected] have problem to handle this use.

Here the demo: https://github.com/morlay/ts-node-issue ([email protected] and [email protected]).

Run ts-node index.ts and will get:

⨯ Unable to compile TypeScript:
index.ts(1,25): error TS7016: Could not find a declaration file for module 'css-color-function'. '/Users/morlay/GitRepo/ts-node-issue/node_modules/css-color-function/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/css-color-function` if it exists or add a new declaration (.d.ts) file containing `declare module 'css-color-function';`

And tsc index.ts work well.

ts-node@6 work well too. may something change at 7.0.0 and break it.

invalid

Most helpful comment

@blakeembrey Got it .Thanks.
However, for other usage with ts-node/register
or some tools which are base on node-interpret like webpack (https://webpack.js.org/configuration/configuration-languages/#typescript).

We have to put TS_NODE_FILES=true before webpack --config webpack.config.ts.

All 16 comments

Please see the README, check the changelog and search existing issues. A major release does mean something changed and something broke backward compatibility.

I was interested to see why TypeScript worked in this case and found this:

======== Module name 'css-color-function' was successfully resolved to '/Users/blakeembrey/Projects/tmp/ts-node-issue/node_modules/css-color-function/lib/index.js'. ========

It's not finding your type definition either, but apparently it is using the .js file instead.

Oh, of course! Once you specify files via the CLI it's not even using your tsconfig.json file anymore. I continuously forget this fact. So, essentially, it's compiling in non-strict mode anyway where .js files are ok. If I disable strict it replicates fine.

@blakeembrey
tsc without special file work well too.

TypeScript will find my declaration file for type checker (and final resolve to .js file).
If I removed or just named the module name like

declare module "css-color-function2"

TypeScript will throw error.

index.ts:1:25 - error TS7016: Could not find a declaration file for module 'css-color-function'. '/Users/morlay/GitRepo/ts-node-issue/node_modules/css-color-function/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/css-color-function` if it exists or add a new declaration (.d.ts) file containing `declare module 'css-color-function';`
1 import { convert } from "css-color-function"

TypeScript will scan local declaration file.
It told me can do like that (...or add a new declaration (.d.ts) file containing declare module 'css-color-function').

In this case, ts-node is different from typescript.
Maybe we couldn't disable strict to solve this.

Btw. declaration file in node_modules works well.

Like I said, please check the README, changelog and issues. I did not suggest disabling strict as a solution. I said it's equivalent to the situation you presented me. If you want to read your definition, use types or paths such as linked in the README.

@blakeembrey Got it .Thanks.
However, for other usage with ts-node/register
or some tools which are base on node-interpret like webpack (https://webpack.js.org/configuration/configuration-languages/#typescript).

We have to put TS_NODE_FILES=true before webpack --config webpack.config.ts.

@blakeembrey
Could we added this https://github.com/gulpjs/interpret/pull/51 to README to should why these changes?

Even the Tip on the first line, for most TypeScript users, it still be confused for these changes.
We haven't configure any files/includes/excludes in tsconfig.json.
[email protected] ignore files/includes/excludes defaults of TypeScript configuration too.

May just have some example to show how to fix as migration guide.

For example. I put all my type declarations under __types__ folder, I can just added paths into my tsconfig.json , avoiding to set TS_NODE_FILES=true

{
  "compilerOptions": {
      "paths": {
          "*": [
              "*",
              "__types__/*"
          ]
    }
  }
}

Feel free to submit a PR doing so. I hoped it was clearer with the two links and explicit mentions of paths and types. The reasons might better belong in the “how it works” section.

Even the Tip on the first line, for most TypeScript users, it still be confused for these changes.

Can only agree here: I had to read this issue and the README a few times to actually figure out where things were going wrong. I'm still not sure (beginning user, got to maintain a ts project recently), so I just decided to use TS_NODE_FILES=true and leave it at that.

My tsconfig.json:

{
    "compilerOptions": {
        "outDir": "./dist",
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": true,
        "noImplicitThis": true,
        "noUnusedLocals": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "sourceMap": true,
        "strict": true,
        "target": "es2016",
        "typeRoots": [
            "server/types/*"
        ],
        "types": [
            "node"
        ]
    },
    "include": [
        "./server/**/*"
    ]
}

The error I'm getting:

server/search.ts(3,24): error TS7016: Could not find a declaration file for module 'level-rocksdb'. '..../node_modules/level-rocksdb/level-rocksdb.js' implicitly has an 'any' type.
  Try `npm install @types/level-rocksdb` if it exists or add a new declaration (.d.ts) file containing `declare module 'level-rocksdb';`

I _do_ have such a .d.ts file in server/types/level-rocksdb/index.d.ts, and I suspect the issue here is not so much with the typeRoots (which according to the README would be considered), but rather with the include part somehow.

Type roots doesn’t take a wildcard - it only takes a directory that contains the types in folders.

I faced the same error when using a wildcard in paths like so:

        "paths": {
            "*": [
                "src/types/*"
            ]
        }

Explicitly mapping the module name to its corresponding declaration file fixes this:

        "paths": {
            "*": [
                "src/types/*"
            ],
            "my-module": ["src/types/myModule.d.ts"]
        }

Is this the intended behavior?

That’s TypeScript’s intended behaviour, so yes. Make sure you put your definitions in the correct locations if you want TypeScript to be able to map to it - that means a directory with an index file.

@blakeembrey I still don't understand as typescript is intended to support declaring types from any file with the .ts extension.

Make sure you put your definitions in the correct locations if you want TypeScript to be able to map to it.

Does this mean all typescript projects using ts-node would have to be refactored to your structure for ts-node to work even though it runs fine with tsc?

I ask because I have several projects with /types/ folders with the types files within them, for instance types/job.types.ts.

@michaelfitzhavey See https://github.com/gulpjs/interpret/pull/51#issuecomment-399490947. If you want the behavior of all files, use the --files option - that's what it's there for. But the basic usage typically doesn't need all the files in a project to work (e.g. gulp scripts).

@blakeembrey even with --files it breaks the run with some errors which is working without issues in previous versions. What the take on this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JoseLion picture JoseLion  Â·  3Comments

dakom picture dakom  Â·  3Comments

nehalist picture nehalist  Â·  3Comments

max-block picture max-block  Â·  4Comments

Borewit picture Borewit  Â·  3Comments