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

r0estir0bbe picture r0estir0bbe · Jan 4, 2017 · Viewed 15.3k times · Source

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?

Answer

Amid picture Amid · Jan 4, 2017

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');