Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an application-specific config file for TypeScript?

Tags:

I am working on a web application using TypeScript. For some parts of the application, I would like to define a simple configuration file that specifies certain aspects (for example the color palette).

How would I create such a configuration file? In JavaScript, creating config.js and referencing it in the HTML code is a very simple solution. What would be the best solution for TypeScript? Maybe a separate Config.ts file?

like image 662
r0estir0bbe Avatar asked Jan 04 '17 15:01

r0estir0bbe


People also ask

How do I create a new config file in Visual Studio 2019?

In Solution Explorer, right-click the project node, and then select Add > New Item. The Add New Item dialog box appears. Expand Installed > Visual C# Items. In the middle pane, select the Application Configuration File template.


1 Answers

You can do it the following way.

First define structure of your config file, in lets say config.ts:

export interface Config {     uriPath: string;        port: number;        settings: string[]; } 

Then create config.json that actually holds the data:

{     "uriPath": "mgNation",     "port": 1506,     "settings": [         "=[tor.start]",         "=[copp.start]"     ] } 

And consume it in your app.ts:

let config: Config = require('./path/to/config.json'); 
like image 119
Amid Avatar answered Sep 21 '22 15:09

Amid