1

Let's say I have a translations module, and I only want to import the French translation file. I want to do something like this:

import french = module("./translations").french;

similar to how you can do with a require in CommonJS.

Is it possible to do something like that in TypeScript, or will I have to import the entire module and just do something like this?

import translations = module("./translations");
var french = translations.french;

2 Answers 2

2

This is actually something that is fairly reasonable to do in some circumstances. Unfortunately, this is not supported by TypeScript at the moment; the grammar for import declarations doesn't allow for dotting off of it (see spec section 9.2.2). This is something we could look at improving in the future. I recommend filing an issue on the Codeplex site.

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

Comments

1

If you only need part of your module, it sounds like your module design might be wrong - i.e. you should have a module that represents what you need and move the stuff you don't need into another module.

For example, have a module per language.

3 Comments

I agree. The language example given in the question could well be designed with a hierarchical module structure such that separate languages correspond to submodules.
That doesn't really address the question: often you aren't free to edit the module being used.
@Roly in that case I would be tempted to write adapters over the module - or perhaps a language factory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.