Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ts-node can't find my type definition files

When I run ts-node node_modules/jasmine/bin/jasmine I get these errors:

tsc/globals.ts:7:12 - error TS2304: Cannot find name 'SugarcubeState'.

7     State: SugarcubeState;
             ~~~~~~~~~~~~~~

Here is that global file:

/* eslint-disable @typescript-eslint/no-explicit-any */
console.log("global.ts");

// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace NodeJS {
  interface Global {
    State: SugarcubeState;
    setup: {};
  }
}

declare const State: SugarcubeState = {
  variables: {}
};

declare const setup: any = {
  variables: {}
};

Here is my index.d.ts:

type SugarcubeVariables = {
};

type SugarcubeState = { variables: SugarcubeVariables };

These are both in the same directory and Visual Studio code is not complaining about anything. Why can't ts-node seem to find my type definition files?

I googled this and found this website: https://github.com/TypeStrong/ts-node#help-my-types-are-missing Following its advice, i modified my tsconfig file to have a

"typeRoots": ["tsc"],                       /* List of folders to include type definitions from. */

in it, but it had no effect on the error. I also tried this:

    "types": ["tsc/index.d.ts"],                           /* Type declaration files to be included in compilation. */

But again, no difference in the errors I received. How do I get ts-node to recognize my .d.ts files?

PS: If you're wondering why I'm defining things this way, see this answer https://stackoverflow.com/a/43523944/61624


I reread that link and it appears that I need to have a very specific directory structure. The problem is, it says I need but the module name in this directory structure, and given the way I wrote my index.d.ts, i have no idea what to name this directory.

like image 982
Daniel Kaplan Avatar asked Mar 20 '20 22:03

Daniel Kaplan


People also ask

Does TS-node use TSC?

json (recommended)​ ts-node automatically finds and loads tsconfig. json . Most ts-node options can be specified in a "ts-node" object using their programmatic, camelCase names.

Should you use TS-node?

No you shouldn't use it in production, even though it will cache the compiled files it'll be slower to start and consume more memory because it keeps around an instance of the compiler and files in memory.

What is TS-node in package json?

TS-Node is a Node. js package that we can use to execute TypeScript files or run TypeScript in a REPL environment. To compile and run TypeScript directly from the command line, we need to install the compiler and ts-node globally.

What is TS-node command?

ts-node is a TypeScript execution engine and REPL for Node. js. It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node. js without precompiling. This is accomplished by hooking node's module loading APIs, enabling it to be used seamlessly alongside other Node.


1 Answers

Enable ts-node --files option in tsconfig.json like this:

{
  "ts-node": {
    "files": true
  },
  "compilerOptions": {
    // ....
  }
}

Further Reading:

  • Help! My Types Are Missing!
  • ts-node ignores d.ts files while tsc successfully compiles the project
like image 184
saulpalv Avatar answered Sep 22 '22 03:09

saulpalv