Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to setup tsconfig "files" vs "include"?

Tags:

I was wondering what is better and what are the pros and cons of using "files" vs "include" in tsconfig?

I don't really like the include pattern because is just including all ts files in the src folder, and I might now want that.

I like the "files" approach, because I can point to the entry file and just load everything that file needs.

I'm using typescript with webpack. I guess the entry point is defined in webpack so there is no need to define in typescript as well?

I tried to use "files" but looks like there is no way to set a folder to look for custom type definitions: typescript with tsconfig with "files" => import image module not found

like image 893
Totty.js Avatar asked Aug 13 '18 09:08

Totty.js


People also ask

What should I exclude in Tsconfig?

Use the exclude option in your tsconfig. json file to exclude a folder from compilation in TypeScript. The exclude option changes what the include setting finds and defaults to node_modules and bower_components .

What is include in Tsconfig?

tsconfig include The include property specifies the files to be included in the TypeScript project. You can either include files or file paths like ./src/** to specify what should be included.

Why Tsconfig is needed?

The presence of a tsconfig. json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig. json file specifies the root files and the compiler options required to compile the project.


1 Answers

There are two examples of tsconfig.json presented on the official website of TypeScript,—one with "files" property, another one with "include" and "exclude" properties specified:

Using the "files" property

{     "compilerOptions": {         // irrelevant     },     "files": [         "core.ts",         "sys.ts",         "types.ts",         "scanner.ts",         "parser.ts",         "utilities.ts",         "binder.ts",         "checker.ts",         "emitter.ts",         "program.ts",         "commandLineParser.ts",         "tsc.ts",         "diagnosticInformationMap.generated.ts"     ] } 

Using the "include" and "exclude" properties

{     "compilerOptions": {         // irrelevant     },     "include": [         "src/**/*"     ],     "exclude": [         "node_modules",         "**/*.spec.ts"     ] } 

So, basically, "files" is used to specify separate files directly by their path, while "include" and "exclude" is used to target collections or groups of files or folders etc.

like image 68
Dima Parzhitsky Avatar answered Sep 19 '22 15:09

Dima Parzhitsky