0

I'm using TypeScript internal modules to structure my code into logical blocks. (much like a C# assembly or a Java jar).

When doing so, the TypeScript classes within such an internal module are split across different files (one file per class). However, referencing classes inside this module only works when those classes are exported.

Naturally, I'd like to keep some of those classes internal to my module. For example, suppose I'd like to keep both of these classes internal.

/// Bicycle.ts
module Sample {

    class Bicycle {
    }
}

/// Mountainbike.ts
/// <reference path="Bicycle.ts" />
module Sample {

    class Mountainbike extends Bicyle {
    }
}

Using visual studio 2013, this only compiles when putting an export on the Bicycle class. When I put both classes in a single file, everything works fine. Am I missing something here?

Thanks for your time, Koen

1 Answer 1

0

There isn't a concept of "module internal" in TypeScript. Things are either visible from outside your immediate module declaration (export) or not (no export).

If you have some set of types you'd like to be left alone, you can structure your code so it's clearer to consumers which types should not be used:

module Sample {
    export module Internal {
        export class Bicycle { }
    }
}

/* Somewhere else */
module Sample {
    export class MountainBike extends Internal.Bicycle {

    }
}

Note that since modules can always be re-opened, this approach is just as 'safe' as it would be if there were some concept of module-internal. You can think of this as analogous to how there isn't a concept of a "public namespace" or "private namespace" in C#.

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

1 Comment

The main problem with this approach is that all your internal submodules, which are implementation details, automatically end up in your public .d.ts for everyone to consume - creating dependency on things that should be merely artifacts. What's worse, with declaration emit turned on, it throws errors (TS4025, TS4029) forcing you to export all related internal elements, so you can't even see what's public or internal to your own codebase anymore. It's a horrible mess with apparently no good answer at this time :-(

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.