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 1 year has passed since last update.

TypeScriptのBarrel(バレル)パターンてなに?

Posted at

バレルパターンというファイルの管理の仕方がTypeSctiptにあるようです。
TypeScript Deep Dive 日本語版

  • Barrelの意味:樽

従来のモジュールのimportの仕方

3つのモジュールをimport

demo/fizz.ts
const Fizz = () => {
  console.log('Fizz')
}
export Fizz
demo/buzz.ts
const Buzz = () => {
  console.log('Buzz')
}
export Buzz
demo/fizzbuzz.ts
const FizzBuzz = () => {
  console.log('FizzBuzz')
}
export FizzBuzz

importする文が3つ必要になる
→これを1つにまとめることができるのがBarrelパターン

demofile.ts(Barrelパターン前)
import { Fizz } from '../demo/fizz'
import { Buzz } from '../demo/buzz'
import { FizzBuzz } from '../demo/fizzbuzz'

そのために必要なindexファイルを作成

demo/index.ts
export * from './fizz'; 
export * from './buzz'; 
export * from './fizzbuzz'; 
export FizzBuzz

1つのファイルからimport

demofile.ts(Barrelパターン後)
// ../demo/indexのindexは省略することが可能
import { Foo, Bar, Baz } from '../demo';
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?