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
- TypeScript: ver. 3.8.2
- tsc: ver.1.20150623.0
Resources
Compiler Options - TypeScriptTypeScript: 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.
- When I only updated the update date of the file, the version wasn't changed.
- 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) - After changing the directory of the project and delete node_modules and "npm install", and "tsc -b" again, the version wasn't changed.
- When I changed the file as same as old version, the version of tsconfig.tsbuildinfo was also changed as same as old one.
- After deleting tsconfig.tsbuildinfo and "tsc -b" again, the versions weren't changed.(The output files were created.)
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.
コメント
コメントを投稿