0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TypeScript奮闘記 No.04 『Module編』

Last updated at Posted at 2021-04-03

Module

JavaScriptにModuleが導入された

ES6形式のModuleを使用する事ができるが、それ以外の場合は トランスパイルが必要。

  • Asynchronous Module Definition (AMD)
    • ブラウザ向けのコードによく使用される
    • define を使ってModuleを定義する
  • CommonJS
    • Node.jsで使用される形式
    • Moduleの定義には module.exportsを使用し依存関係を定義するには require を使用する
  • Universal Module Definition(UMD)
    • Module定義のスタイルを統一的に書けるようにしたい、という試みから生まれた(ってことらしい
    • ブラウザとNode.jsの両方で使用できる
  • ES6
    • exportキーワードでモジュールを定義し、importで依存関係を定義する

Export

Moduleからコードをエクスポートすると、そのコードを他のモジュールで使用できるようになる。

export interface Item {
    name: string;
    unitPrice: number;
}

またこのような書き方もできる

 interface Item {
  name: string;
  unitPrice: number;
}

export { Item }

import

import { Item } from "./item

// use Item Interface..

default

defaultキーワードを使うことでデフォルトでエクスポートできるタスクをいっこに限定する

export default interface Item {
    name: string;
    unitPrice: number;
};

次はもっと詳細にかいてみる

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?