What is the purpose of tsconfig.json? - json

I was reading Angular2 references and found this: tsconfig.json.
I would like to know what the following parameters mean?
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]
}

The tsconfig.json file corresponds to the configuration of the TypeScript compiler (tsc).
These links could give you details about these attributes:
http://www.typescriptlang.org/docs/handbook/tsconfig-json.html
http://json.schemastore.org/tsconfig
https://angular.io/docs/ts/latest/guide/typescript-configuration.html#!#tsconfig
Here are some hints:
target: the language used for the compiled output
module: the module manager used in the compiled output. system is for SystemJS, commonjs for CommonJS.
moduleResolution: the strategy used to resolve module declaration files (.d.ts files). With the node approach, they are loaded from the node_modules folder like a module (require('module-name'))
sourceMap: generate or not source map files to debug directly your application TypeScript files in the browser,
emitDecoratorMetadata: emit or not design-type metadata for decorated declarations in source,
experimentalDecorators: enables or not experimental support for ES7 decorators,
removeComments: remove comments or not
noImplicitAny: allow or not the use of variables / parameters without types (implicit)

tsconfig.json signifies the directory in which it is kept is the root of TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project.
The compiler is expected to execute as per the configurations mentioned:
"target": "es5" => will compile the es6 to es5 so that it is compatible browsers.
"module": "system" => specifies the module code generations (commonjs', 'amd', 'system', 'umd', 'es6' etc)
"moduleResolution": "node" => Determine how modules get resolved
"sourceMap": true => Generates corresponding ‘.map’ file so that it can be used in the production code for debugging.
"removeComments": false => Remove all comments except copy-right header comments beginning with /*!
"noImplicitAny": false => Raise error on expressions and declarations with an implied ‘any’ type.
If the "exclude" property is specified, the compiler includes all TypeScript (*.ts or *.tsx) files in the containing directory and subdirectories except for those files or folders that are excluded.

Already there are lot of answers, but I would like to add one more point as why tsconfig required. As per angular docs
TypeScript is a primary language for Angular application development.
It is a superset of JavaScript with design-time support for type
safety and tooling.
Browsers can't execute TypeScript directly. Typescript must be
"transpiled" into JavaScript using the tsc compiler, which requires
some configuration.
Typically, you add a TypeScript configuration file called tsconfig.json to your project to guide the compiler as it generates JavaScript files.
For more information https://angular.io/guide/typescript-configuration

tsconfig file indicates the project as typescript project and it includes options on how the typescript files to be compiled. For details check the site https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Most of points are covered above. some are missed which i like to highlight.
tsconfig.json will tell where is build code and which version to target.
For instance, when it goes to production it will refer the below key in tsconfig.json and pick the build.
"outDir": "./dist/out-tsc", --> where to locate the build file.
And our browser do not understand typescript so mention which type of js to convert our code which will be understood by browser.
In other words, we write our code in typescript but bring that code to es5, We do that using the below field.
"target": "es2015",

As you know browsers can accept only javascript files, But when you use angular you don't use javascript. Instead you use typescript files .. so now we need a method to change those typescript files to js files.
It's done by tsconfig.json file that specifies the configuration options that we need to do this change . Such as compiler options and the root files .

Related

TypeScript Ignore compilation of a folder, but keep its contents for type-checking

I'm making an app using plain HTML and TypeScript (compiled into JS ofc). I'm not using anything external and I'm also not using TS modules - I had issues with calling methods from global scope.
I have the following folder structure:
- build
- page
- index.html
- src
- include
- <bunch of .ts files>
- tsconfig.json
- <few .ts files>
- tsconfig.json
This is the \src\tsconfig.json file:
{
"compilerOptions": {
"outDir": "../build/",
"target": "ES6",
"lib": ["DOM", "esnext"],
"downlevelIteration": true
},
"compileOnSave": true
}
This is the \src\include\tsconfig.json file:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outFile": "../../build/Include.js",
}
}
What I want
I want to compile the contents of \src\include\ into a single file to \build\include.js because manually linking many script files in \build\page\index.html would be dumb.
The rest of the .ts files in \src can be compiled into individual files to \build\.
The way it's set up right now works, but the compilation of the \src\ folder (I'm compiling each folder separately) also compiles the \src\include\ folder into \build\include\<individual compiled files>.js
What I've tried
If I exclude the \src\include\ from \src\tsconfig.json, I can't compile it because the classes from the include folder cannot be found in the .ts files in \src\
Other SO question answers I've tried:
https://stackoverflow.com/a/69568072/19674384 - Classes from the include folder cannot be found despite being explicitly included (wtf?)
https://stackoverflow.com/a/40781823/19674384 - Classes from the include folder still cannot be found
https://stackoverflow.com/a/65282934/19674384 - noEmit doesn't compile anything, the second suggestion seems like something else entirely
Am I doing something wrong or should I approach this completly differently?

Stackblitz, Angular Including JSON file not working

I'm using Stackblitz: https://stackblitz.com/edit/bible-talk
I'm using Angular 10.
I'm trying to import data from a JSON file.
I found an example on how to do this on Stackblitz:
https://stackblitz.com/edit/json-import-example?file=src%2Fapp%2Fapp.component.html
You're supposed to add these to compiler options to tsconfig.json:
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
I've done that: https://github.com/JaredMathis/bible-talk/commit/449f6269b85bb53eb5aa245bd709075db4307163
I'm still getting this error:
Error in src/app/app.component.ts (2:18)
Cannot find module '../assets/kjv/John.json' or its corresponding type declarations.
I made a brand-new Stackblitz. It works there. https://stackblitz.com/edit/json-import-not-working?file=tsconfig.json
Stackblitz does not support static assets at the moment.
See: https://github.com/stackblitz/core/issues/72
Workaround: You have to host your static assets somewhere else and link them in your code appropriately.
Your I think import in your github version is wrong, it starts with ./ where as your working stackblitz does not.
import data from './../assets/John.json';
Try removing the first ./

What's resolutions and overrides in a `bower.json` file?

In a bower.json file, what are the resolution and overrides properties used for?
{
"name": "name",
"dependencies": {
"angular": "~1.4.8",
...
"jquery": "2.2.4"
},
"overrides": {
"ionic": {
"main": [
"release/js/ionic.js",
"release/js/ionic-angular.js"
]
}
},
"resolutions": {
"angular-ui-router": "~0.2.15",
"angular": "~1.5.3"
}
}
Resolution
The resolution section appears when you need to resolve dependency versions (after bower install) when conflicts occur. It's for making a decision regarding which concrete version of a dependency to use when the need to resolve dependency conflicts arises - bower automatically injects this decision as the "resolution" record. So the next time a conflict occurs (when updating the dependency tree, etc), the resolved version will be based on the "resolution" data in your configuration file.
Overrides
Overrides section is used to override the file(s) references when pointing to dependent library.
Task runners in most cases use the bower configuration library metadata to inject links to these libraries into a page's content. When we want to inject a bootstrap link into a page, we do not need to go into the "bower_components" folder, find the package, and investigate the file content. We can use the component metadata to find the main, injectable file reference.
The "overrides" section is used to change this data to use another file, or even a set of files, as a package's main entry point.
Multiple Bower packages can list different versions of the same library as a dependency. The resolutions section specifies which version of the library to use whenever this type of situation occurs. If not specified in bower.json, you will receive a command line prompt upon running bower install.
The overrides section makes it possible to override default paths to assets installed through Bower when using a task runner like Gulp. If you intend to move files from their default location in the bower_components folder to accommodate your build process, for example, it could prove handy in this type of setup.
We use resolutions object in your bower.json file to specify the component name & version to automatically resolve the conflict when running bower commands.
Overrides section is used to override the file(s) references when pointing to dependent library.

VS Code .es6 extensions can't find module

Some projects I work on use .es6 extension for JavaScript files that import/export using ES6 module syntax.
While webstorm and webpack seem to have no issues with this setup, VSCode gives a red squiggly saying [js] Cannot find module './filename.es6'.
Is there some way to get VS Code to find modules imported that do not have a .js extension? We would like to use .jsx in a similar fashion.
I have this for a .jsconfig:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs"
},
"files": [
"**/*.jsx",
"**/*.es6",
"**/*.js"
]
}
...and tried adding .es6 under user settings for VSCode, but suspect I did it wrong or that doesn't solve the issue.

Compiling external JS files with Cljsbuild in ClojureScript

I'm trying to compile some JS libraries that we have with lein-cljsbuild to integrate them in our ClojureScript code base. First I added some goog.provide in top of each file, and the files are hierarchically organised in a directory tree according to their namespace (like in Java). That is namespace a.b.c is in src-js/libs/a/b/c.js
I have put the JS files in the root directory of the projects in src-js/libs, and I have the following :compiler options for lein-cljsbuild:
{:id "prod",
:source-paths ["src-cljs" "src-js"]
:compiler
{:pretty-print false,
:libs ["libs/"]
:output-to "resources/public/js/compiled-app.js",
:optimizations :simple}}
None of the JS files get compiled into the compiled-app file. What's wrong?
I also tried to put them in resources/closure-js/libs without success.
I'm using lein-cljsbuild 0.3.0.
First, unlike what is suggested in some texts, you do not need to include your private closure library locations in any classpath configuration statement in your project.clj. So unless the "src/js" directory included in your "source-paths:" statement is for some other purpose, you can remove it.
Second, the only thing to add to your project.clj, for the sake of bringing in your private closure code, is the "libs:" reference you have made; BUT unlike what you have entered, that reference must be to a specific *.js file (or files) and not merely a directory. So if the library you want to used is in a file named test.js and that resides in the /src/js directory, your libs: entry would be: "src/js/test.js". See the cljs-build release notes if you want to use that plugin's default :libs directory option.
Third, (and it looks like you know this already, but this is what tripped me up) if you are using a browser-backed REPL (repl-listen option of cljsbuild), you still will not be able to load/reference/use your private library assets from that REPL until you include a :require statement somewhere in the source for your compiled-app.js (e.g. "(ns testing (:require [myprivatelib]))" ), THEN you must re-compile (lein cljsbuild once) and reload your browser page with a link to compiled-app.js. This brings in that code base. Otherwise, your browser REPL will just keep insisting that the namespace provided in your closure library is not defined.
I hope this helps.