1

I am new to Typescript and module handling. In my project I am coding in Typescript for developing a browser library. I am therefore using AMD. Following is the tsconfig.json file.

{
  "compilerOptions": {
    "target": "ES5",
    "outDir": "out",
    "module": "amd"
  },
  "files": [
    "src/main.ts",
    "src/communication.ts",
    ...
  ]
}

File communication.ts is:

export module MyProj.DataExchange {
  export interface Communication {
    connect(uri: string): void;
    close(): void;
    status: int;
  }
}

Referencing components from other files

I want to use Communication in main.ts:

import communication = require('./communication');
export module MyProj {
  export class Communicator 
    implements communication.MyProj.DataExchange.Communication {
    ...
  }
}

I would like to avoid using the whole signature communication.MyProj.DataExchange.Communication. So I tried something like:

import communication = require('./communication').MyProj.DataExchange;

But it did not work.

My questions

I have a feeling I am doing something wrong in here. Here my questions:

  • Am I doing things the way they are supposed to be done?
  • How to avoid using the whole name for an imported component?
  • In case you might tell me not to use module, I need to separate my components into namespaces. So how to correctly set namespaces in case I am doing it wrong?
1
  • Why did you put the Communication interface within a module? Just eliminate the outer module and use communication.Communication. Commented Mar 30, 2016 at 22:47

1 Answer 1

1

In Typescript 1.4 have been introduced Type Aliases.

Your code might be adapted to use aliases like this:

import communication = require('./communication')
type Communication = communication.MyProj.DataExchange.Communication;
export module MyProj {
  export class Communicator 
    implements Communication {
    ...
  }
}
Sign up to request clarification or add additional context in comments.

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.