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

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.

michael-land, micalevisk, changxiaoqiang, mattn9x, sambunting, can-cc, dimazollo, tsulatsitamim, nickhaughton, msegers, and 30 more reacted with thumbs down emoji prevostc, ClaudiuCeia, sslgeorge, garkin, bachbach, slavafomin, danieldaeschle, kiprasmel, peanutbother, jeafleohj, and 3 more reacted with laugh emoji All reactions

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.

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

keenwon, MichalLytek, nozzlegear, can-cc, ani-naslyan, anishpateluk, and jlonardi reacted with thumbs up emoji keenwon reacted with hooray emoji keenwon reacted with heart emoji All reactions

@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 gulpjs/interpret#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__/*"

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.

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?

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