Can't export constant in Typescript
Asked Answered
E

2

52

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?

Errol answered 8/9, 2016 at 8:19 Comment(4)
See this question: #32647715Chantalchantalle
I do export const hello = "dd"; like in your link but it doesn't workErrol
In Node.js or in a browser?Eyelet
in a browser (chrome and firefox the same)Errol
H
67

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.

Hussein answered 1/11, 2017 at 13:45 Comment(0)
L
1

// hi.ts

export const hello: string = "dd";

You must also specify the type of the const.

Larvicide answered 10/1 at 16:40 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Broadminded

© 2022 - 2024 — McMap. All rights reserved.