Having compiled my TypeScript project successfully, I intended to run it in VS Code's debug mode using ts-node
. Problem is, ts-node
can't find d.ts
files I created (while tsc
has no problem with it).
Project structure is:
/ conf/ dist/ src/ types/ package.json tsconfig.json
tsconfig.json
relevant entries are:
{ "compilerOptions": { "target": "es2017", "module": "commonjs", // "lib": [], "sourceMap": true, "outDir": "dist", "rootDir": "src", "moduleResolution": "node", "baseUrl": ".", "paths": { "*": [ "node_modules/*", "src/types/*" ] }, // "rootDirs": [], // "typeRoots": [], // "types": [], }, "include": [ "src/**/*" ] }
The definition file ts-node
can't find is src/types/global.d.ts
:
import { App } from '../App'; declare global { namespace NodeJS { interface Global { app: App; } } }
So, trying to run it with ts-node
I see:
TSError: ⨯ Unable to compile TypeScript: src/boot.ts(15,59): error TS2339: Property 'app' does not exist on type 'Global'.
How to resolve it globally? I've found that /// <reference path="./types/global.d.ts" />
does the trick but I'd have to repeat it in every file using global.app
.
My TypeScript version is 3.0.1
d. ts is the type definition files that allow to use existing JavaScript code in TypeScript. declare function sum(a: number, b: number): number; From now on, we can use the function in TypeScript without any compile errors.
d. ts files are used to provide typescript type information about a module that's written in JavaScript, for example, underscore / lodash / aws-sdk. This will allow you to use the javascript modules without the need to convert them to ts without getting any type of error on your code.
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. We recommend this because it works even when you cannot pass CLI flags, such as node --require ts-node/register and when using shebangs.
ts-node can be helpful to run a script and get results without having to worry about file changes and compilation. But before you can execute TypeScript files, you'll first have to install Node. JS on your machine to set up an environment to run ts-node.
I was having a similar problem, but I could not add --files
, because I run ts-node
by registering the module through mocha
(i.e. mocha -r ts-node/register ...
).
I could solve it by adding a files
and a ts-node
section to tsconfig.json
like this:
// tsconfig.json { "ts-node": { "files": true }, "files": [ "src/index.ts", "src/global.d.ts" ], "compilerOptions":{ //... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With