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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With