45

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

Totty.js
  • 15,563
  • 31
  • 103
  • 175

2 Answers2

33

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.

Parzh from Ukraine
  • 7,999
  • 3
  • 34
  • 65
  • 2
    Can both be used at the same time? – Marek Oct 28 '20 at 13:49
  • 10
    @Marek: yes – and stuff in "files" will never be ruled out by "exclude" patterns, if you add any, whereas stuff from "include" will. (If you add just a "files" array and expect all else you had before to stay as it was, also add the default rule `"include": ["**/*"]` that you got for free when you had no "files" rule.) – ecmanaut Oct 30 '20 at 18:41
0

There are some interesting considerations and examples about that in the documentation: link. Basically the "files" is used if there isn't a large amount of file to be included.