Typescript: Automatic barrel by folder, unless an index.ts is explicitly provided

Created on 7 May 2019  ·  4Comments  ·  Source: microsoft/TypeScript

Using an index.ts in the root directory of each folder to export it's contents is a great way to organise your projects.

However explicitly providing a barrel in each folder then exporting each file in that barel is both cumbersome, cluttering and prone to mistakes.

What I'd like to see is each folder has all of it's exports available to import, unless that folder has an index.ts and explicitly re-exports.

/my-module
    index.ts
    part-1.ts
    part-2.ts
    part-3.tsx

The above folder structure would be replaced with

/my-module
    part-1.ts
    part-2.ts
    part-3.tsx

and the import statement could still be

import {} from './my-module'

For an implementation example/reference, check out Go (Golang). Achieving a similar behaviour to it would be nice.

Out of Scope Suggestion

Most helpful comment

That sounds like a great idea!

All 4 comments

That sounds like a great idea!

You'd need your module loader to do this (eg, node's require or the browser's resolution mechanisms) - we don't actually much around with your imports and exports beyond compiling them to the appropriate format.

What about generating an indexs.js file with all the exports by default if there is no index.ts file present in the input? If you want to not export anything you would create an empty one.

This may be marked as out of scope by the typescript team, but an additional tool that does the job would have a lot of potential. Just that it would be much easier and better to have it integrated on a flag in the ts compiler.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmaurer picture wmaurer  ·  3Comments

bgrieder picture bgrieder  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

siddjain picture siddjain  ·  3Comments

weswigham picture weswigham  ·  3Comments