スキップしてメイン コンテンツに移動

[TypeScript] About tsconfig 1

Intro

On the section 2 of "Effective TypeScript" had been written about setting "noImplicitAny", "strictNullChecks" and etc.
And I wanted to know more about the tsconfig.json.
What could I do through the settings of it?

This time, I wrote about "allowJs", "checkJs", "incremental", and "tsBuildInfoFile".

Environment

  1. TypeScript: ver. 3.8.2
  2. tsc: ver.1.20150623.0

Resources

Compiler Options - TypeScript
TypeScript: TSConfig Reference - Docs on every TSConfig option
Effective TypeScript

Default tsconfig.json

When I executed "tsc --init", I could get the default tsconfig.json.

tsconfig.json


{
  "compilerOptions": {
    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    // "sourceMap": true,                     /* Generates corresponding '.map' file. */
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    // "outDir": "./",                        /* Redirect output structure to the directory. */
    // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "composite": true,                     /* Enable project compilation */
    // "tsBuildInfoFile": "./",               /* Specify file to store incremental compilation information */
    // "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    "strict": true,                           /* Enable all strict type-checking options. */
    // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */

    /* Module Resolution Options */
    // "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
    // "allowUmdGlobalAccess": true,          /* Allow accessing UMD globals from modules. */

    /* Source Map Options */
    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */

    /* Advanced Options */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  }
}

"allowJs" and "checkJs"

When the project hadn't only TypeScript but also JavaScript, I could use them.

allowJs

By default, TypeScript hadn't been able to call JavaScript class, function etc.
For instance, if I had had these files, I couldn't compile unless setting "allowJs": true.

js-sample.js


export class JsSample {
    call(message) {
        console.log(message);
    }
}

program.ts


import { JsSample } from './js-sample'; // compile error
export function start() {
    const sample = new JsSample();
    sample.call('Hello world');
}

So when I converted from JavaScript to TypeScript, I would use it.

checkJs

By default, the types of the variables, arguments, etc. of the JavaScript function had treated as "any" from TypeScript.
So when I called "call(message)" of JsSample, I could write like "call()", "call(9)" etc.
If I made "checkJs": true, the compiler suggested to add the @param.

js-sample.js


export class JsSample {
    /**
     * @param {string} message
     */
    call(message) {
        console.log(message);
    }
}

After adding the @param, TypeScript could know the type of the argument and I could get errors when I wrote like "call()", or "call(9)".
And I also could know the type in JavaScript.
But because the type checker didn't work, so I had to write carefully in JavaScript.

js-sample.js


export class JsSample {
    constructor() {
        this.call(); // no compile errors 
    }
    /**
     * @param {string} message
     */
    call(message) {
        console.log(message);
    }
}

"incremental" and "tsBuildInfoFile"

From TypeScript 3.4, we had been able to incremental build.
TypeScript 3.4 · TypeScript
Announcing TypeScript 3.4 RC | TypeScript
Announcing TypeScript 3.4 | TypeScript

When I set "incremental": true, the compiler would compile only the file what had been changed.
Especially there had been so many files, this made quickly for the compiling.

How did know the changes?

When the "incremental" was enabled, the file the named "tsconfig.tsbuildinfo" was created at "outDir" by default.
I could changed the file name and output directory by "tsBuildInfoFile" of tsconfig.json.

tsconfig.tsbuildinfo


{
  "program": {
    "fileInfos": {
      "../node_modules/typescript/lib/lib.d.ts": {
        "version": "49ff9798f592c8b7e628fd881401e68810c1b3589ecd7a41b32b3c287374cde0",
        "signature": "49ff9798f592c8b7e628fd881401e68810c1b3589ecd7a41b32b3c287374cde0"
      },
      "../node_modules/typescript/lib/lib.es5.d.ts": {
        "version": "b42eddba1a53c9d27279cfe7fc0416c10a81489826ad47e39013b9d340fc0cc7",
        "signature": "b42eddba1a53c9d27279cfe7fc0416c10a81489826ad47e39013b9d340fc0cc7"
      },
      "../node_modules/typescript/lib/lib.dom.d.ts": {
        "version": "f2039e0dbc31f69183f6377b368c033802dfdcf77e5fd214def06c1fd7b225ed",
        "signature": "f2039e0dbc31f69183f6377b368c033802dfdcf77e5fd214def06c1fd7b225ed"
      },
      "../node_modules/typescript/lib/lib.webworker.importscripts.d.ts": {
        "version": "fe4e59403e34c7ff747abe4ff6abbc7718229556d7c1a5b93473fb53156c913b",
        "signature": "fe4e59403e34c7ff747abe4ff6abbc7718229556d7c1a5b93473fb53156c913b"
      },
      "../node_modules/typescript/lib/lib.scripthost.d.ts": {
        "version": "b9faa17292f17d2ad75e34fac77dd63a6403af1dba02d39cd0cbb9ffdf3de8b9",
        "signature": "b9faa17292f17d2ad75e34fac77dd63a6403af1dba02d39cd0cbb9ffdf3de8b9"
      },
      "../src/js-sample.js": {
        "version": "15ae3298ba783372f4d0dfca3ce48d97a9f01b3bef782b99b4166c46c997f311",
        "signature": "98dce6f3d4f808200ff78c9885cd6c7bb96ea4495799b65727b8c8f6763995a3"
      },
      "../src/program.ts": {
        "version": "612fb02a2d0baa56ac37c1ccf7d9968b14ae759438fd8b97661f07eaaec12116",
        "signature": "a45d5e7a4141f06d386bc57cd656348c63bfb7e6baa80b8e6798eebffd6ff1ea"
      }
    },
    "options": {
      "incremental": true,
      "target": 1,
      "module": 1,
      "allowJs": true,
      "outDir": "./",
      "strict": true,
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "watch": true,
      "configFilePath": "../tsconfig.json"
    },
    "referencedMap": {
      "../src/program.ts": [
        "../src/js-sample.js"
      ]
    },
    "exportedModulesMap": {},
    "semanticDiagnosticsPerFile": [
      "../node_modules/typescript/lib/lib.d.ts",
      "../node_modules/typescript/lib/lib.es5.d.ts",
      "../node_modules/typescript/lib/lib.dom.d.ts",
      "../node_modules/typescript/lib/lib.webworker.importscripts.d.ts",
      "../node_modules/typescript/lib/lib.scripthost.d.ts",
      "../src/program.ts",
      "../src/js-sample.js"
    ]
  },
  "version": "3.8.2"
}

When I had changed program.ts, the version of "../src/program.ts" was changed and update program.js of "outDir".
But how did the compiler know the change?
I had tried something and got these results below.
  1. When I only updated the update date of the file, the version wasn't changed.
  2. After I had deleted the file of "outDir" and "tsc -b" again, the output file wasn't created.

    (It was only created when I changed the file)
  3. After changing the directory of the project and delete node_modules and "npm install", and "tsc -b" again, the version wasn't changed.
  4. When I changed the file as same as old version, the version of tsconfig.tsbuildinfo was also changed as same as old one.
  5. After deleting tsconfig.tsbuildinfo and "tsc -b" again, the versions weren't changed.(The output files were created.)
According to the document, these infomations were stored on the disk.
https://www.typescriptlang.org/v2/en/tsconfig#incremental
But I couldn't found where they were.
I had thought I had to read the source code of tsc.

コメント

このブログの人気の投稿

[Angular][ASP.NET Core] Upload chunked files

Intro I wanted to send files to Web application (made by ASP.NET Core). If the file size had been small, I didn't need do any special things. But when I tried to send a large file, the error was occurred by ASP.NET Core's limitation. Though I could change the settings, but I didn't want to do that, because I hadn't known the file sizes what would been actually using. So I splitted the data into chunks first, and sent them. After receiving all chunks, I merged them into one file. There might be some libraries or APIs (ex. Stream API) what did them automatically, but I couldn't find them. What I did [ASP.NET Core] Make CORS enabled [Angular] Split a large file into chunks [Angular][ASP.NET Core] Send and receive data as form data [ASP.NET Core] Merge chunks into one file [ASP.NET Core] Make CORS enabled Because the client side application(Angular) and the server side application(ASP.NET Core) had been separated, I had to make CORS(Cross-Origin Requests)

[Nest.js] Use WebSocket with ws

Intro Until last time , I had used node-web-rtc to try WebRTC. But because the example was a little complicated for I understood the core functions of using WebRTC. So I look for other frameworks or libraries. PeerJS is a famous library for WebRTC. peers/peerjs: Peer-to-peer data in the browser. - GitHub peers/peerjs-server: Server for PeerJS - GitHub PeerJS - Simple peer-to-peer with WebRTC A problem is I don't know how to integrate to the Nest.js project. I couldn't find examples. So I don't choose at least this time. What shall I choose? According MDN, WebRTC doesn't specify strictly what technology is used on server application for connecting two devices. Signaling and video calling - Web APIs | MDN But in many examples include MDN's one use WebSocket. samples-server/s/webrtc-from-chat at master · mdn/samples-server · GitHub So I try WebSocket in the Nest.js project. Use WebSocket in a Nest.js project Nest.js has a function for using We

[Nest.js] Show static files

Intro I wanted to use Nest.js and WebRTC(node-webrtc). NestJS - A progressive Node.js framework Documentation | NestJS - A progressive Node.js framework And because I wanted to try with simple page(not use JavaScript frameworks), I added static HTML, CSS, JavaScript into a Nest.js project. Prepare Install First, I installed @nestjs/cli. First steps | NestJS - A progressive Node.js framework As same as last time , I couldn't do global install because I had used Volta. But I could installed by volta. volta install @nestjs/cli Create project nest new nest-web-rtc-sample volta pin node@12 Run npm start After doing "npm start", I could getting "Hello World!" from http://localhost:3000. Add static files I could add static files by two ways. @nestjs/serve-static First one of them was using "serve-static". Serve Static | NestJS - A progressive Node.js framework npm install --save @nestjs/serve-static And I needed adding a module into app.modu