// demo/foo.ts
export class Foo {}
// demo/bar.ts
export class Bar {}
// demo/baz.ts
export class Baz {}
バレルがなければ、ユーザーは3つのインポート文を必要とするでしょう:
import { Foo } from '../demo/foo';
import { Bar } from '../demo/bar';
import { Baz } from '../demo/baz';
代わりに、以下を含むdemo/index.tsバレルを追加することができます:
// demo/index.ts
export * from './foo'; // re-export all of its exports
export * from './bar'; // re-export all of its exports
export * from './baz'; // re-export all of its exports
今、ユーザーは必要なものをバレルからインポートできます:
import { Foo, Bar, Baz } from '../demo'; // demo/index.ts is implied
// demo/index.ts
export * from './foo'; // re-export all of its exports
export * from './bar'; // re-export all of its exports
import * as baz from './baz'; // import as a name
export { baz }; // export the name
そして今、ユーザーは次のようになります:
import { Foo, Bar, baz } from '../demo'; // demo/index.ts is implied
// usage
baz.getBaz();
baz.setBaz();
// etc. ...