Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't export constant in Typescript

Can someone help me please

I have 2 files main.ts and hi.ts

hi.ts:

export const hello = "dd"; 

main.ts:

import { hello } from "./hi"; ... class A {     public sayHello() {         console.log("hello=" + hello);     }     ... } 

I have exception:

Uncaught ReferenceError: hello is not defined(…)

How can I see this const variable from class A? Is it possible?

like image 980
Egor Avatar asked Sep 08 '16 08:09

Egor


People also ask

How do I export a constant in TypeScript?

To re-export values from another file in TypeScript, make sure to export the name exports as export {myFunction, myConstant} from './another-file and the default export as export {default} from './another-file' . The values can be imported from the file that re-exported them.

Can constants be exported?

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

How do you export functions in TypeScript?

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 you declare a constant in TypeScript?

Typescript constants are variables, whose values cannot be modified. We declare them using the keyword const . They are block-scoped just like the let keyword. Their value cannot be changed neither they can be redeclared.


1 Answers

My answer refers to TypeScript 2+.

// 1.ts export const AdminUser = { ... }  // index.ts import * as users from './docs/users/admin'; var adminUser = users.AdminUser; 

The only difference between your code and mine is the * operator in the import statement.

like image 116
Socratees Samipillai Avatar answered Sep 25 '22 01:09

Socratees Samipillai