Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: How to export a variable

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

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

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

I do it by:

import {arr} from './file1';

However, when I want to access 'arr', I can't just write 'arr', but I have to write 'arr.arr'. The first one is for the module name. How do I access directly an exported variable name?

like image 763
CrazySynthax Avatar asked Mar 01 '17 19:03

CrazySynthax


People also ask

How do I export a variable in TypeScript?

Use named exports to export multiple variables in TypeScript, e.g. export const A = 'a' and export const B = 'b' . The exported variables can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

What is export {} in TypeScript?

TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

How do I export a TypeScript function?

Use named exports to export a function in TypeScript, e.g. export function sum() {} . The exported function can be imported by using a named import as import {sum} from './another-file' . You can use as many named exports as necessary in a single file.

How do I export a type in typescript?

Use a named export to export a type in TypeScript, e.g. export type Person = {}. The exported type can be imported by using a named import as import {Person} from './another-file'. You can have as many named exports as necessary in a single file. Here is an example of exporting a type from a file called another-file.ts.

What is typescript and how does typescript work?

TypeScript also encourages dynamic typing of variables. This means that, TypeScript encourages declaring a variable without a type. In such cases, the compiler will determine the type of the variable on the basis of the value assigned to it.

How to declare a variable in typescript?

Variable Declaration in TypeScript S.No. Variable Declaration Syntax & Descriptio ... 1. var name:string = ”mary” The variable st ... 2. var name:string; The variable is a strin ... 3. var name = ”mary” The variable’s type is ... 4. var name; The variable’s data type is an ...

How to import variables exported from another file?

The exported variables can be imported by using a named import as import {A, B} from './another-file'. You can have as many named exports as necessary in a single file.


Video Answer


2 Answers

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

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

For a named export you can try something like:

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

Then to import you could use the original statement:

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

This will get around the need for arr.arr you mentioned.

How do pirates know that they are pirates? They think, therefore they ARR!!

like image 131
Kieran Avatar answered Oct 20 '22 18:10

Kieran


If you do:

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

...

import arr from './file1';

Then it should work

like image 33
Mikael Gidmark Avatar answered Oct 20 '22 19:10

Mikael Gidmark