Typescript: how to export a variable

I want to open 'file1.ts' and write:

export var arr = [1,2,3]; 

and open another file, say 'file2.ts' and directly access 'arr' in file1.ts:

I'm doing it:

 import {arr} from './file1'; 

However, when I want to access 'arr', I cannot just write 'arr', but I need to write 'arr.arr'. The first is for the name of the module. How to access directly the name of the exported variable?

+38
import module export typescript
source share
2 answers

If you do:

 var arr = [1,2,3]; export default arr; 

...

 import arr from './file1'; 

Then it should work

+49
source share

There are two different types of export: named and default .

You can have multiple named exports per module, but only one export by default.

For named exports, you can try something like:

 // ./file1.ts const arr = [1,2,3]; export { arr }; 

Then you can use the original statement to import:

 // ./file2 import { arr } from "./file1"; console.log(arr.length); 

This will save you the need for arr.arr , which you mentioned.

+45
source share

All Articles