简体   繁体   中英

Imports with typescript

I begin a new project with typescript and I have questions about imports. Let's assume I have these 2 files:

// foo.ts
namespace root {
    export class Test {
        constructor() {
            console.log(BAR)
        }
    }
}

// a/bar.ts
namespace root.a {
    export const BAR = 'Whatever'
}

How can I use my BAR const into my Test class? I tried to add /// <reference path="a/bar.ts" /> and then I can call root.a.BAR or even a.BAR .

But I would prefer to not use this reference statement, is it possible? Also, I would like to use BAR without its full namespace, is it also possible?

Thank you in advance,
Xavier

I would prefer to not use this reference statement, is it possible

Yes, you would use tsconfig.json : https://basarat.gitbook.io/typescript/project/compilation-context/tsconfig

but beware of ordering pain (you will need to order using files ) : https://basarat.gitbook.io/typescript/content/docs/tips/outFile.html

Instead use modules.

More

https://basarat.gitbook.io/typescript/content/docs/project/modules.html

Thank you for your answer, basarat.

I use namespaces because I don't want to set up something like RequireJS, required by modules . But if I understand well, it is not the right way to solve my problem.

I tried to follow this example (typescript + angular 1): hottowel-angular-typescript by John Papa but is it really a good example? I mean, I really don't want to prefix every class / interface / const ... by its namespace!

Is there another solution without file modules?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM