Robin gupta
Robin gupta

Reputation: 196

How to use ts file in external js file

How i can use a ts file in java script file. I have an angular project in that i have a constant file which is environment.ts file and data is as below

export const Environment = {
    PRODUCTION: false

};

and i have used one external java script library and in that library i want to access this environment.ts file i.e want to use constant.

i have tried with

var en = require('./environment.ts');

But not able to access it.

Upvotes: 1

Views: 609

Answers (3)

Aditya Mittal
Aditya Mittal

Reputation: 1771

What I did is created dev.json and prod.json with my configurations.

I then imported dev.json into environment.ts and prod.json into environment.prod.ts for exporting to ts files.

I also imported dev.json into an environment.js and environment.prod.js for exporting to js files. You could also use the json directly if you want, but this will give you the flexibility to manipulate in js if you want later.

Now to update configs you just keep updating the json files.

Make sure your external js knows which env configs to read depending on how you're building/compiling it.

Upvotes: 0

Anji
Anji

Reputation: 715

Try this,

import { Environment } from './environment';

Now we can access the properties like Environment.PRODUCTION

Upvotes: 0

Bojan Ivanac
Bojan Ivanac

Reputation: 1180

The problem probably comes from the fact that TS is compiled into JS during the build process.

Since you're just exporting the single object, it doesn't really need to be a typescript file.

Try to rename the file into 'environment.js', it shouldn't break anything and it will allow you to require it in other js files.

Upvotes: 1

Related Questions