tsconfig.json

The majority of compilation options in tsconfig.json in the project do not affect the compilation of Typescript.

Therefore, some options need to configured carefully to make the IDE's inspection function consistent with the compilation behavior in Cocos Creator 3D.


The following options should not be modified explicitly:

  • compilerOptions.target
  • compilerOptions.module

For example, if tsconfig.json is set to:

{
    "compilerOptions": {
        "target": "es5",
        "module": "cjs"
    }
}

Script code:

const myModule = require("path-to-module");

It will not cause an error in the IDE (using tsc as a checker) because compilerOptions.module is set to cjs. However, the implicit compilerOptions.module in Cocos Creator 3D is es2015, therefore, it may prompt errors such as required undefine at runtime.

Script code:

const mySet = new Set();

This is legal in Cocos Creator 3D, but the IDE may report an error: Because compilerOptions.target is set to es5: ES6 introduced Set.


It is also possible to freely modify options.

For example, when it is needed to prohibit the use of implicit any in all Typscript scripts in your project.

Set compilerOptions.noImplicitAny to true in tsconfig.json, as using an IDE (such as Visual Studio Code), the corresponding error prompt will be received.


For most projects, some options in tsconfig are fixed. For example, compilerOptions.target, compilerOptions.module and Cocos Creator 3D type declarations, file location, etc.

Due to the good design of tsc, the extends option allows tsconfig.json to be cascadable. Cocos Creator 3D supports this, therefore, the fixed tsconfig option is placed under {project path}/tmp/tsconfig.cocos.json and managed by Cocos Creator 3D.

Therefore, tsconfig.json under the project root path can be configured as follows to share these fixed options:

{
    extends: './tmp/tsconfig.cocos.json',
    compilerOptions: {
        /* Custom tsconfig.json options*/
    }
}

Fortunately, when you create a new project, the editor will automatically generate a tsconfig.json file automatically.

results matching ""

    No results matching ""