LoginSignup
6
3

More than 5 years have passed since last update.

TypeScript moduleの2つの記述形式

Last updated at Posted at 2017-01-15

書き方2つ

1.es6形式
2.「export = and import = require()」 形式 (何か名前ある?)

es6形式(ECMAScript2015形式)

  • 例)
export interface StringValidator {
    isAcceptable(s: string): boolean;
}
import { ZipCodeValidator } from "./ZipCodeValidator";
  • 新しくモジュールを使う場合はこちらで良い
  • TypeScript1.5から導入された模様

「export = and import = require()」 形式

  • 例)
class ZipCodeValidator {
   ...
}
export = ZipCodeValidator;
import zip = require("./ZipCodeValidator");
  • 「export =」 で、公開する要素を指定する。
  • 「export =」 は、1つモジュールで1回しか使えない。(export single object)
  • 「import xxx = require」 で、使用する要素を指定する。
  • CommonJS/AMDのexportsオブジェクトの考え方に合わせているが、互換性はない。
  • この形式のモジュールを"tsc --module es6"でコンパイルすると、以下のエラーが出る。
error TS1203: Export assignment cannot be used when targeting ECMAScript 2015 modules. 
Consider using 'export default' or another module format instead.

(moduleをes6形式で書くか、諦めて"--module CommonJS"などにしなさいと言う意味)

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3