1

I'm playing around with TypeScript and export/import.

I simply want to have a file-structure like in C#. I want to nest a helper-class whic only contains some constant string-properties. This is what the file ~/Scripts/Helper/UrlHelper.ts looks like:

module Helper.UrlHelper {
    abstract class Controller {
        protected static readonly controllerName: string;
    }

    export abstract class Account extends Controller {
        static controllerName: string = "Account";

        static readonly loginGet: string = [Account.controllerName, "Login"].join("/");
        static readonly loginPost: string = [Account.controllerName, "Login"].join("/");
    }
}

My goal is to use the values e.g. Helper.UrlHelper.Account.loginPost. But I don't get the import/export to work.

My last try was to add export * from "./Helper/UrlHelper" and use the import import {Account} from "../Helper/UrlHelper";

This leads to the error:

Module '".../Scripts/Helper/UrlHelper"' has no exported member 'Account'

I tried nearly everything from here, but nothing worked. What am I doing wrong?

2 Answers 2

1

Do not use the keyword module, just export the members you need to import elsewhere:

// Helper/UrlHelper.ts
abstract class Controller {
    protected static readonly controllerName: string;
}

export abstract class Account extends Controller {
    static controllerName: string = "Account";

    static readonly loginGet: string = [Account.controllerName, "Login"].join("/");
    static readonly loginPost: string = [Account.controllerName, "Login"].join("/");
}

Then, the exported member Account can be imported:

// other-directory/other-file.ts
import { Account } from "../Helper/UrlHelper"

The documentation on modules is here:

You could also use namespaces but it isn't an ES6 standard. If you really want namespaces, here is the documentation:

See also:

Sign up to request clarification or add additional context in comments.

3 Comments

On top of this answer, another helpful tip is that import's also work at the folder level if you have an index file in the folder, e.g. import { Account } from '../Helper'; If you have a file called index.ts in the Helper folder. In the index file, you will need to include the code export * from './UrlHelper';. Handy for then only needing 1 line for importing all your helpers.
@TimBJames Interesting. But is it described somewhere? Is it a robust solution that works with all protocols (AMD, CommonJS, ES6) and all bundlers/loaders?
I am not 100%, some more info is here though stackoverflow.com/questions/44028806/…
0

Referencing this: https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html
You're advised to not wrap classes in modules as it can become unnecessarily confusing.
A better option would be:

abstract class Controller { /* ... */ }
export abstract class Account extends Controller { /* ... */ }

This is could be placed in a Helper naming the file UrlHelper.ts as suggested.
Then later on when you want to import the class, you do:

import { Account } from "./Helper/UrlHelper";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.