Reputation: 1212
So I try to be a good boy and do things right, and currently I thought I would write a rather simple application to update database tables, and as the tables belong to an application, and have to be spot on, but very fiddly, I thought I would write it all in typescript.
I wen't through part of the Typescript course on edx.org, seemed easy enough, but I have an issue translating a simple config export to a class.
Basically, I am trying to replace a simple config object (would have looked like this in Javascript).
./config/config.js
modules.exports = {
'lokiLogDb': "./database/log.db",
'lokiCfgDb': "./database/cfg.db"
}
I would call it like var config = require('./config/config');
So my new ./config/config.ts
export class config {
private _lokiLogDb : string = "./database/log.db";
private _lokiCfgDb : string = "./database/cfg.db";
private _lokiCfgKey : string = "Th1sC0uldUnl0ckYourK3y";
get lokiLogDb () {
return this._lokiLogDb;
}
get lokiCfgDb () {
return this._lokiCfgDb;
}
set lokiCfgKey (passwd: string ) {
return "Not ready yet";
}
get lokiCfgKey () {
return "Not ready yet"
}
}
OK, maybe a bit over the top, but worth testing I thought. So next step is to import the values: import * as config from './config/config';
And that is where it all falls to pieces. I can not read any values from my module. What am I doing wrong, or what have I missed?
======= UPDATE =======
So my updated config.ts
export default class config {
private _lokiLogDb : string = "./database/log.db";
private _lokiCfgDb : string = "./database/cfg.db";
private _lokiCfgKey : string = "secretcode";
get lokiLogDb () {
return this._lokiLogDb;
}
get lokiCfgDb () {
return this._lokiCfgDb;
}
set lokiCfgKey (passwd: string ) {
return "Not ready yet";
}
get lokiCfgKey () {
return "Not ready yet"
}
}
And the beginning of my app.ts
import * as debugFactory from 'debug';
import * as http from 'http';
import * as express from 'express';
import * as path from 'path';
import * as logger from 'morgan';
import * as bodyParser from 'body-parser';
import config from './config/config';
console.log("Will connect to ", config )
//import localDB from './modules/localdb';
import sampleRoute from './routes/sample';
And the output I get is
Will connect to function config() {
this._lokiLogDb = "./database/log.db";
this._lokiCfgDb = "./database/cfg.db";
this._lokiCfgKey = "secret";
}
But I cant get to any of the variables, am I just doing this in a silly way?
Upvotes: 0
Views: 3206
Reputation: 47675
Try with:
export default class config
in your config.ts
and import it with:
import {config} from './config/config'
.
Also, make sure you are really transpiling your typescript to javascript.
Upvotes: 1