3

I just recently started with typescript,

I was moving my javascript code to typescript. In my JS code, I had something like this

export const envVar = {
    SOCIAL_URL: false,
    BASE_CONFIG_URL: '', //we change it based on the request
    BASE_CONFIG_CLIENT_WEB_URL: base_config.client_url || 'http://localhost:3000',
    DB_HOST: db_settings.db_host || 'localhost',
    DB_USER: db_settings.db_user || 'root',
    APP_URL: base_config.app_url || 'blle://',
    DB_PASSWORD: db_settings.db_password || '',
    DB_DATABASE: db_settings.db_database || 'blale',
    DB_CONNECTION_NAME: db_settings.connection_name || '',
    FIREBASE_DATABASE_URL: db_settings.firebase_datbase_url,
    FACEBOOK_APP_ID: facebook.app_id || '',
    FACEBOOK_APP_SECRET: facebook.app_secret || '',
    FACEBOOK_REDIRECT_URL_SOCIAL: facebook.redirect_url_social || '',
    FACEBOOK_REDIRECT_URL_AUTH: facebook.redirect_url_auth || '',
    FACEBOOK_REDIRECT_URL: function() {
        return this.SOCIAL_URL ? this.FACEBOOK_REDIRECT_URL_SOCIAL : this.FACEBOOK_REDIRECT_URL_AUTH
    },
    TWITTER_CONSUMER_KEY: twitter.api_key || '',
    TWITTER_REDIRECT_URL_AUTH: twitter.redirect_url_auth || '',
    TWITTER_REDIRECT_URL_SOCIAL: twitter.redirect_url_social || '',
    TWITTER_APP_SECRET: twitter.app_secret_key || '',
    TWITTER_REDIRECT_URL: function() {
        return this.SOCIAL_URL ? this.TWITTER_REDIRECT_URL_SOCIAL : this.TWITTER_REDIRECT_URL_AUTH
    },
    JWT_SESSION_DURATION: jwt_config.session_duration || '7d',
    JWT_ISSUER: jwt_config.issuer || 'blle',
    JWT_SECRET: jwt_config.secret || '933dnz82'
}

moved this from module.exports to export const. Now, how can I use it another .ts file? I initially did something like this

import * as envVar  from "./../config"

const { JWT_SECRET, JWT_ISSUER, JWT_SESSION_DURATION } = envVar

but this is giving following error

Property 'JWT_SESSION_DURATION' does not exist on type 'typeof

2
  • I don't see JWT_SESSION_DURATION in the list of your constants. Commented Nov 19, 2019 at 6:06
  • @AnirudhBagri 3rd last from in the envVar object Commented Nov 19, 2019 at 6:23

2 Answers 2

3

Because you are doing a named export, your import will receive an object containing envVar as one of the property.

So you can do the following.

import { envVar }  from "./../config"

OR

import * as envVar  from "./../config"

const { JWT_SECRET, JWT_ISSUER, JWT_SESSION_DURATION } = envVar.envVar

If you want to use the existing import, then you have to change your named export to default export.

const envVar = {....};
export default envVar;
Sign up to request clarification or add additional context in comments.

Comments

0
// GlobalConfig .ts
export namespace GlobalConfig {
    export var JWT_SESSION_DURATION = { myValue: "4" }
    export const JWT_SECRET = "asasasasas"
} 

And then you can use this in another file like this. Note this is one of the many ways you can do this.

//MyCode.ts
import { GlobalConfig } from "./GlobalConfig";

export class SomeComponent {
    current_jwt = "";

    constructor() {
        // some init code
        this.current_jwt = GlobalConfig.JWT_SESSION_DURATION.myValue;
    }

    ngOnInit() {
        // something
        console.log(this.current_jwt);
        console.log(GlobalConfig.JWT_SECRET);
    }

}

2 Comments

why do we use namespace in typescript?
a namespace is like a package in Java or put simply a substitute for a fully qualified class. Thus while importing the global variables you don't have to create an instance of anything. You can also separate out variables based on use, type etc to ensure security and cross-reaching concerns by creating mulitple namespaces per domain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.