Alwaysblue
Alwaysblue

Reputation: 11830

typescript does not exist on type 'typeof object

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

Upvotes: 3

Views: 3487

Answers (2)

Abhisek Mishra
Abhisek Mishra

Reputation: 269

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;

Upvotes: 3

damitj07
damitj07

Reputation: 2899

// 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);
    }

}

Upvotes: 0

Related Questions