简体   繁体   中英

Referencing a file (module) in Typescript using AMD

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?

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 {
    ...
  }
}

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