JavaScript Support
allowJs
Default:
false, unlesscheckJsis setReleased: 1.8
Related: checkJs,emitDeclarationOnly
Allow JavaScript files to be imported inside your project, instead of just .ts and .tsx files. For example, this JS file:
When imported into a TypeScript file will raise an error:
Imports fine with allowJs enabled:
This flag can be used as a way to incrementally add TypeScript files into JS projects by allowing the .ts and .tsx files to live along-side existing JavaScript files.
It can also be used along-side declaration and emitDeclarationOnly to create declarations for JS files.
checkJs
Released: 2.3
Related: allowJs,emitDeclarationOnly
Works in tandem with allowJs. When checkJs is enabled then errors are reported in JavaScript files. This is the equivalent of including // @ts-check at the top of all JavaScript files which are included in your project.
For example, this is incorrect JavaScript according to the parseFloat type definition which comes with TypeScript:
// parseFloat only takes a string
module.exports.pi = parseFloat(3.142);When imported into a TypeScript module:
// @filename: constants.js
module.exports.pi = parseFloat(3.142);
// @filename: index.ts
import { pi } from "./constants";
console.log(pi);TryYou will not get any errors. However, if you turn on checkJs then you will get error messages from the JavaScript file.
// @filename: constants.js
module.exports.pi = parseFloat(3.142);Argument of type 'number' is not assignable to parameter of type 'string'.
// @filename: index.ts
import { pi } from "./constants";
console.log(pi);TrymaxNodeModuleJsDepth
- Released: 2.0
The maximum dependency depth to search under node_modules and load JavaScript files.
This flag can only be used when allowJs is enabled, and is used if you want to have TypeScript infer types for all of the JavaScript inside your node_modules.
Ideally this should stay at 0 (the default), and d.ts files should be used to explicitly define the shape of modules. However, there are cases where you may want to turn this on at the expense of speed and potential accuracy.