5

I got an issue in typescript. I have the following hierarchy :

.
+-- @types
|   +-- declaration1.ts
|   +-- declaration2.ts
+-- common
|   +-- utils1.ts
|   +-- utils2.ts
+-- tests
|   +-- utils1.test.ts
|   +-- utils2.test.ts

In my tsconfig.json, i put this configuration to have all my types exposed in both tests and commons (or vscode complains) :

{
  "compilerOptions": {
    "outDir": "build/",
  },
  "include": ["tests", "common", "@types"]
}

My problem is, that i don't want to compile all this stuffs, i just want to compile the commons (which are utils, i cannot get a single (or even multiple) entrypoint(s)).

So i'd like to type tsc and get a build folder fill like this :

.
+-- build
|   +-- common
|   |   +-- utils1.js
|   |   +-- utils2.js
// nothing else

Instead i got this :

.
+-- build
|   +-- @types
|   |   +-- declaration1.js
|   |   +-- declaration2.js
|   +-- common
|   |   +-- utils1.js
|   |   +-- utils2.js
|   +-- tests
|   |   +-- utils1.test.js
|   |   +-- utils2.test.js

I cannot get how to exclude this directories from compiles.

Thanks if anyone has an idea.

oopinou
  • 143
  • 1
  • 1
  • 9

3 Answers3

1

Try creating new tsconfig.json under the folder you don't want to build and add below:

{
  "compilerOptions": {
    "noEmit": true
  },
  "exclude": ["src/**/*.ts"]     // files you want to build
}

and include files you want to build in original tsconfig.json:

{
  "compilerOptions": {
    ...
  },
  "include": ["src/**/*.ts"]     // files you want to build
}
Terry
  • 101
  • 1
  • 4
0

To include types but not compile them, add them to "typeRoots" in your tsconfig.json. Everything in "include" will be compiled.

{
  "compilerOptions": {
    "typeRoots": [ "@types", "tests" ]
  }
}

To ignore files or directories use the "exclude" option, for example:

{
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}
Felix B.
  • 166
  • 8
  • If i do so i got the error `Cannot find type definition file for 'common'.`. VSCode also complain for not found types – oopinou Mar 14 '19 at 09:13
  • You can use modules to import classes in your common files. In your `declaration.ts`: `export class Declaration { … }`. In your `utils.ts`: `import * as Declaration1 from '../@types/declaration1`. You can now use `Declaration1.Declaration` to access your class/type. There is also no need to include `typeRoots` in your `tsconfig.json` anymore. – Felix B. Mar 14 '19 at 09:41
0

You could create a new tsconfig.json under the folders you don't want to build, add 'noEmit: true' in it.

Garry Xiao
  • 232
  • 3
  • 9