I personally like TypeScirpt, write code with Ts to be more comfortable than Js too much, can greatly improve the efficiency of writing code, reduce the workload of code maintenance. Because I have the foundation of C++ and C#, I can get started with Ts quickly. Although there is no difficulty in using it, but for a long time, did not go to understand the compilation process and configuration of typescirpt, so they can not be considered to really understand it.
If you don’t know much about ts configuration and compilation process, then this article should help you.
The next part requires your environment to have TypeScript installed. Please make sure that TypeScript is installed, otherwise the tsc
command will not be recognized.
I.tsc
Ts provides a command line function tsc
, which can be used to compile Ts files into Js files.
First create a folder and index.ts
file and go to the learnTsConfig
folder.
You can enter some Ts code in the index.ts
file, e.g.
Then use the tsc [ts file]
command to compile the file.
|
|
As you can see, there is an index.js
file generated in the same directory as src/index.ts
, which is the compiled file, and the contents of the src/index.js
file are as follows.
Specifying the ts file for the tsc command is not applicable to the project, we can’t add all the file paths to be compiled after the tsc command, so we usually use JSON configuration file (tsconfig.json) to configure the tsc compiler. When executing the tsc
command, the tsc compiler will first look for the tsconfig.json
file in the current directory. If there is no tsconfig.json
file in the current directory, it will keep looking up the directory until it finds a tsconfig.json
file, the directory where the tsconfig.json
file is located is project’s root directory.
If you do not find a tsconfig.json
file, the tsc
command will not compile any files, but will only output the version number and help information.
The tsconfig.json file is described in detail below.
II. The tsconfig.json
file
The tsconfig.json
file supports JSON5 content, so you can add comments to the tsconfig.json
file, using single quotes instead.
The structure of the tsconfig.json
file is as follows.
In the following I will describe the configuration options divided into compiled options (configuration items under the compilerOptions field) and non-compiled options (root-level options outside the compilerOptions field).
First, under the learnTsConfig
directory, create a new tsconfig.json
file.
|
|
Non-compile options
Non-compiled options usually control information about the items (files) to be compiled by the typescript compiler, such as the ts files to be compiled.
-
files
files
option is used to indicate which files need to be compiled, you can add a set of file paths, support relative paths and absolute paths, relative paths are relative to the project root directory, that is, the directory where the tsconfig.json file is located, it is recommended that you use relative paths, avoid using absolute paths, so that even if the project changes the environment, you do not have to change the configuration.Create a new file
outerIndex.ts
in the root directory, and the directory structure will be as followsEnter the following in the tsconfig.json file.
Execute the
tsc
command to compile, and the compiled file structure will be as follows. -
include
&exclude
If there are few files in the project, using the
files
option to set the target files to be compiled is a good choice, but when there are a lot of files or the project files are updated frequently, writing all the files to be compiled in thefiles
array is a bit tricky, and you have to update thefiles
option every time you add a ts file.So ts also provides
include
option, which is similar tofiles
, but you can use pattern match to delete the previously compiled js files and change the contents of thetsconfig.ts
file as follows.Execute
tsc
compilation, the file structure is as follows, onlyindex.ts
in thesrc/
directory is compiled.exclude
does the opposite ofinclude
, it is used to exclude certain files, it also supports pattern matching, clean up the js file and update thetsconfig.ts
file as follows.The
include
option means that all ts files in the root directory are compiled, andexclude
means that all ts files in thesrc/
directory are excluded, so the compiled file structure oftsc
is as follows.By default typescript will automatically look for
.ts
files and.d.ts
files if you set theallowJS
compile option (described later) to true, and will also look for.js
files if you set theallowJS
compile option (described later) to true.src/**/*
is equivalent tosrc/**/*.ts
andsrc/**/*.d.ts
(andsrc/**/*.js
ifallowJs
is true).There are some folders that typescript will automatically exclude, such as
node_modules
,bower_components
,jspm_packages
and<outDir>
. If you want to force ts to compile ts files in these folders, you need to specify thefiles
file option.
Compiler Options
Compiler options are the relevant configuration under the compilerOptions
field.
Output files
-
outDir
By default, ts compiled js files are in the same directory as the source files. Use the
outDir
option to specify the directory where the compiled files are located. Clean up the js files generated by previous compilation.Update the
tsconfig.json
file.Then execute the
tsc
command to compile, after compiling, thedist/
folder will be generated, thedist/
directory is the compiled js file, the structure of the directory is the same as the project directory structure. -
rootDir
The default root directory of typescirpt project is the directory where the
tsconfig.json
file is located, and all relative paths are relative to this root directory. We can change the project’s root directory location with therootDir
option. Delete the dist folder and update thetsconfig.json
file.Execute the tsc command and the compiled directory will be as follows.
In most cases, we just keep the default behavior of TypeScript and use the
tsconfig.json
file located in the root of the project. -
removeComments
The
removeComments
option is used to remove commented code from the compiled js file.First clean up the project and update the
tsconfig.json
file as follows.Update the contents of the
src/index.ts
file.tsc
is compiled, and the compileddist/src/index.js
file reads as follows.You can see that the comments have not been removed. Update the
tsconfig.json
file.Compile again, and the compiled
dist/src/index.js
file is as follows.You can see that the comment content has been removed.
-
module
Suppose you are developing a project that needs to run in a nodejs environment, and in the project you use
import
to introduce modules, butnodejs
does not support it. nodejs uses the CommonJS module system. To convert the compiled js fileimport
statements torequire
statements, you can configure"module": "CommonJS"
.Here, let’s try it manually. Update the
tsconfig.js
file.Add the add.ts file to the src directory with the following content.
Update the
src/index.ts
file.Compile the file, look at
dist/src/index.js
content as follows, you can see that theimport
statement has been converted to arequire
statement. -
outFile
outFile
can specify that the compiled result file is packaged as a bundle, i.e. a js file, provided that themodule
option is set toSystem
orAMD
. If you want to support other module options, you can use tools like webpack, parcel, etc.For a simple experiment, update the
tsconfig.json
file as follows.Only the
dist/bundle.js
file is generated after compilation.
source map
-
sourceMap
The source-map file represents a mapping relationship between the compiled source file and the output result file, which plays a very important role in debugging the project and allows us to display the source code in the browser developer tools for debugging. If the debugger looks at the compiled code, then debugging is such a painful thing.
The source-map file is automatically generated by the compiler tool and ends with the
.map
suffix, it is a JSON file with the following structure.The
file
field indicates the name of the output file corresponding to the source-map file, and thesources
field indicates the compiled source file. Themapping
attribute is a base64 encoded value that represents the relationship between the source file and the output file.And the compiled output file will have a comment at the end of the
sourceMappingURL
field, indicating the map file location, and this comment will be read by the browser to locate the map file.To generate the source-map file, you can set the
sourceMap
field to true.Update the
tsconfig.json
file.Execute the
tsc
command, which will generatedist/bundle.js
anddist/bundle.js.map
after compilation. -
inlineSourceMap
The
sourceMap
option generates a separate.map
file for the compiled js file. By settinginlineSourceMap
to true, you can avoid generating.map
files and inline the map directly into the compiled js file.Update the
tsconfig.json
file.After compiling, view the compiled
dist/bundle.js
file.You can see that the
sourceMappingURL
is directly followed by the base64 data (map file content).
type declaration
The most important role of typescript is to make js support types and turn it into a strongly typed language.
-
declaration
Set the
declaration
field to true to automatically generate declaration files.Update the
tsconfig.json
file.Executing the tsc command to compile, you can see the
dist/**/*.d.ts
file generated, which is the declaration file for the corresponding*.js
file. With these declaration files, the ts compiler can know the API structure of the package, even if the compiled JavaScript file does not contain any type information.The directory of the generated dist files is as follows.
-
declarationDir
When
declaration
is set toture
, the ts compiler puts the corresponding .d.ts file into the same level as the compiled js file. You can put all declaration files in the same directory by settingdeclarationDir
.Update the
tsconfig.json
file as follows.Execute the tsc command, you can see the
dist/types/
directory is generated, the structure of the directory is the same as the source file directory, except that they are all.d.ts
declaration files. -
lib
typescirpt has some built-in TypeScirpt declaration files, such as
Promise
,Object.freeze
and the declaration of the browser API. These declaration files can provide code hints and warnings. They are usually stored in thelib
folder, which we call the standard library. You can manually select which libraries to import by setting thelib
field. -
typeRoots
When you publish your typescript program, as an npm package, you may publish the compiled JavaScript package, so that non-typescript programs can introduce the npm package, in order to provide declaration information, you can also provide the corresponding declaration file, so that typescript compilers and IDEs (such as vscode) can provide type hints, error hints based on the declaration file. To generate the declaration file, you just need to set
declaration
totrue
.But not all programs are written in typescript, for example lodash, which itself is written in javascript, so it can’t use the function provided by ts to generate the declaration file automatically. So you need to manually provide declaration files for npm packages that don’t have them.
The DefinitelyTyped community’s job is to provide declaration files for the more popular npm packages that don’t have them. You can provide a declaration file for an npm package by installing a declaration file package that starts with
@types
.For example
npm install @types/lodash
,@types/lodash
is the declaration file for thelodash
package.By default, typescript imports all type declarations from the
node_modules/@types
folder into the global space, but note that only declarations in the script file will be imported into the global space, the module file is hidden from the global space.type-root is the directory where the package declaration files are stored, and can be set to a series of file paths via the
typeRoots
option, indicating where typescript imports type information from, the default value isnode_modules/@types
.By setting
"typeRoots": [". /my-types"]
, typescript will only import declarations from themy-types
folder, not from thenode_modules
folder. -
types
typeRoots
is used to import all declarations in the directory into the global space, but iftypes
is set, only the package declarations specified bytypes
will be imported into the global space.
JavaScript compilation
By default, typescript is compiled without the js file. This default behavior can be changed with allowJS
and checkJS
.
-
allowJS
When you introduce a module in a typescript file with an
import
statement, such asimport {add} from ". /add
, by default the ts compiler will automatically look forsrc/add.ts
andsrc/add.d.ts
and it will not considersrc/add.js
, we can change this default behavior by settingallowJS
totrue
.First, update the
tsconfig.json
file as follows.Create a new
src/add.js
and enter the following code.Introduce the
add
function insrc/index.js
.By default,
allowJS
isfalse
, so the compiledadd.js
file will not be included in thedist/src/
directory after the tsc compilation.Update the
tsconfig.json
file to setallowJS
to true.Recompile, and after compiling,
add.d.ts
andadd.js
files will be added to thedist/src/
directory.So, if you want your project to support js files, just set
"allowJs": true
. -
checkJs
Using
allowJS
allows the compiler to include js files in the compilation stage, but the compiler does not type check the js files. Type checking is an important feature of ts that greatly improves our development efficiency, so in order to allow the ts compiler to type check js files, you need to set"checkJS": true
.To explain further, first add
const num = parseInt(1.5);
to the top ofadd.js
.Then compile it with tsc and it compiles successfully.
Then update the
tsconfig.json
file.Compile with tsc again and the compilation should report an error.
Because
checkJS
is true at this point, the ts compiler will type check the js file, and theparseInt
function accepts a string as an argument, but1.5
is of typenumber
, so the type is not compatible, so the compilation fails. If you are using vscode editor withcheckJS
enabled, the editor should give you a smart error when you hit the codeconst num = parseInt(1.5);
.
III. Summary
There are many configuration fields in tsconfig, this article aims to introduce its basic usage and basic structure, and does not cover all of them. I hope that through this article, you can understand tsc compiler, know how to configure tsc compiler and understand its principles.
For more tsconfig configuration, please refer to TSConfig Reference.