Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include a json file when using TypeScript with a tsconfig.json file?

Is there any way to have the TypeScript compiler also copy content files that don't have a ts or tsx extension to the output directory?

For example, my tsconfig.json looks like:

{     "compilerOptions": {         "module": "commonjs",         "target": "es6",         "noImplicitAny": false,         "sourceMap": true,         "outDir": "../../dist",         "types": [         ]     },     "include": [         "file1.ts",         "config.json"     ],     "exclude": [         "../../node_modules"     ] } 

I would like the config.json to end up in my ../../dist directory, but this isn't happening. Is there no concept of a content file for TypeScript?

like image 442
Brian Vallelunga Avatar asked Feb 23 '17 16:02

Brian Vallelunga


People also ask

What is the use of Tsconfig JSON file in TypeScript?

The presence of a tsconfig. json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig. json file specifies the root files and the compiler options required to compile the project.

Does TS node use Tsconfig json?

ts-node automatically finds and loads tsconfig. json . Most ts-node options can be specified in a "ts-node" object using their programmatic, camelCase names. We recommend this because it works even when you cannot pass CLI flags, such as node --require ts-node/register and when using shebangs.

Which of the following option is correct about Tsconfig json?

Correct Answer : Option (C) : This file is used to give the options about TypeScript used for the Angular JS project.


2 Answers

This can be achieved by setting "resolveJsonModule": true in the compiler options and adding "source-path/**/*.json" to the include array in the tsconfig.json

like image 90
Arun Reddy Avatar answered Oct 02 '22 19:10

Arun Reddy


I was having the same problem and the solution for this was to add:

"source-path/**/*.json"

keeping into "includes" array:

"source-path/**/*"

Example:

{   "compilerOptions": {     "module": "commonjs",     "declaration": true,     "removeComments": true,     "emitDecoratorMetadata": true,     "experimentalDecorators": true,     "allowSyntheticDefaultImports": true,     "target": "es2017",     "sourceMap": true,     "outDir": "./dist",     "baseUrl": "./",     "resolveJsonModule": true,     "incremental": true   },   "include":[      "src/**/*",     "src/**/*.json",   ], } 
like image 28
Rodrigo Alcorta Avatar answered Oct 02 '22 18:10

Rodrigo Alcorta