Description
Typescript compiler lacks a native way of maintaining compilation options and source files for a given compilation unit like a project or a module. Using /// can help ease the problem, but there is still a tax of adding and managing the references and it does not handle the compilation options issue. Moreover, different editors have different ways of representing project or module structure and compilation options which make cross-editor collaboration tricky.
The proposal is to introduce a new file .tsconfig
that marks a compilation unit (aka project or module). The .tsconfig follows the footsteps of the .git* file family. Calling tsc in a directory with a .tsconfig will use the contents of the file and all .ts files in the directory as inputs without the need to specify them on the command line again.
.tsconfig in detail
- .tsconfig is a json file
- The presence of a .tsconfig in a directory indicate that all .ts file in this directory and its sub directories recursively are part of the compilation unit
- .d.ts files generated from the compilation but live within the directory are not included in the compilation unit identified by .tsconfig
- .tsconfig can specify additional compilation settings for the compilation
- Calling tsc in a folder containing .tsconfig will trigger compilation using the config file
- Since .tsconfig is a json file, it can be leveraged by other tools (linters, editors, build tools etc..) to store TS related project configuration
- Example .tsconfig
{
"compilerOptions": ["-t ES5", "--out myoutput.js", "--noImplicitAny"],
"linterOptions": {
"suppressWarnnings": [234, 3363]
}
}
Module example
Consider the following directory structure:
shared/
node.d.ts
tools.ts
foo/
.tsconfig
a.ts // adds a ///<reference> to node.d.ts
b.ts
bar/
.tsconfig
c,ts // adds a /// <references> to tools.ts
d.ts
baz/
e.ts
Calling tsc in "foo" is equivalent totsc a.ts b.ts
Calling tsc in "bar" is equivalent to tsc c.ts d.ts baz/e.ts
Using .tsconfig with tsc
- When calling tsc with no input files, the following steps are taken:
- Find the owning .tsconfig file by walking up the directory structure until a .tsconfig file is found, or the drive root is reached
- If a .tsconfig was found, compilation will be triggered with all .ts files in the directory containing the .tsconfig and all its sub directories
- The command line switch are compiled with the configuration options from the .tsconfig defining the compilation command line flags (e.g.:
tsc --watch
will cause a tsc to compile the contents of the current project in watch mode)
- When calling tsc with input a single directory name (e.g.:
tsc compiler
), If the directory contains a .tsconfig at the root, steps 2 and 3 from above are followed.
Specifying output order with --out and .tsconfig
when using --out, the compiler relies on the order of the files passed on the command line to order the emitted JavaScript code. If using .tsconfig, the compiler will look for a special file _references.ts
at the root (next to the .tsconfig file), and will ensure that this file is the first file in the list of input files. this implies that /// tags in _references.ts will specify order of the emitted files.
Open issues:
- Should the tsconfig file have a
.json
extension to indicate intended content, and allow editors to correctly colorize it, format it, etc.. - In a VS project context (say .csproj), should .tsconfig be honored, or ignored in favor of the project.