TypeScript: Can I import a folder without having to write an index.ts file? TypeScript: Can I import a folder without having to write an index.ts file? node.js node.js

TypeScript: Can I import a folder without having to write an index.ts file?


Component isn't a well defined concept in TypeScript & node.js, but module and package are.

In general, module is a source file, let's ignore the exceptions. So by creating index.ts files per directory, you are generating façade modules aggregating only a few file/modules each. If all you are looking to do is organize your source files into logical components, you don't need the per-directory façade, you can simply import each file individually rather than a directory at a time.

At a higher level, if you have a package that consists of a number of different directories, it can have a single index.ts façade at package-level. That file would exported each file/module just once, no need for index.ts per directory. So this might look like (assuming each is a .ts file):

export * from './IntStream';export * from './misc/Interval';export * from './misc/IntervalSet';export * from './Lexer';...


I don't think there's a way to import a directory in TS without and index file

check these questions if you haven't

How to import all modules from a directory in TypeScript?

Typescript 1.8 modules: import all files from folder

I think the best approach is to write a script to generate index.ts that imports all files in the directory, and run that script every time you add/remove a file.