Totty.js
Totty.js

Reputation: 15861

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

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

Upvotes: 52

Views: 34990

Answers (3)

Dacili
Dacili

Reputation: 387

files is used if you want directly to specify the path of files. Usually, if you have a small amount of specific files. If any file from files is missing, you will get an error.

 "files": [
      "src/main.ts",
      "src/polyfills.ts"
    ]

If you want to get a whole bunch of files, and also be able to use glob pattern with wildcards (*, **, ?) then you should use include (and exclude if needed).

"include": [
  "src/**/*.d.ts"
]

If you decide to use both files and include, then if you add exclude, it will affect only your include part, not files.

Upvotes: 3

Alessandro Neri
Alessandro Neri

Reputation: 1

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.

Upvotes: 0

Parzh from Ukraine
Parzh from Ukraine

Reputation: 9913

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.

Upvotes: 38

Related Questions