PierBJX
PierBJX

Reputation: 2353

Import in JS in a specific way

In TS, I have Class1 from class.ts, some functions from helper.ts, some variables from variables.ts:

For example, variables.ts looks like that:

export const test1 = 'test1';
export const test2 = 0;
export const test3 = 'test3';
export const test4 = 'test4';

Then with Webpack, I give the api.ts like the entry to build a module.

api.ts

export { Class1 } from './class1';
export { helper1, helper2 } from './helper';
import * as variables from './utils/common-variables';
export { variables };

Everything is fine when I need all the variables of the variables.ts. I can do this in my file.js:

import { variables } from 'api';

However, sometimes, I only need one variable. So, I would like to know if it was possible to import something like this:

import { variables.test1 as test } from 'api';

Upvotes: 3

Views: 54

Answers (2)

Shishir Naresh
Shishir Naresh

Reputation: 763

You can import a single module from an import and rename it as you want in typescript. It has been mentioned in the documentation, read here

sample:

import { ZipCodeValidator as ZCV } from "./ZipCodeValidator";
let myValidator = new ZCV();`

I hope this helps.

Upvotes: 0

AKX
AKX

Reputation: 169268

No, that particular syntax is not possible – an ImportSpecifier can only really be a name, not an attribute access expression like you have.

You can just import them directly from api/utils/common-variables instead, though, and get rid of the re-export.

Upvotes: 3

Related Questions