Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript 1.8 modules: import all files from folder

I am building a big library with Typescript with like 100 separate ts files. Previously I used export module XXX (renamed to export namespace XXX later) for all my classes, but as books say, this is not a recommended way, I should use import instead.

So I tried importing. This worked fine:

import * as mylib from "./source/source.ts"; 

But as I have 100 files, I don't want to add such a line for all of them. And I want all my classes to be accessible through mylib variable.

So I tried this:

import * as mylib from "./source/"; 

But as soon as I do this, I get: Cannot find module './source/'

Is there a way to import all the classes from a folder with multiple files with a single line?

like image 803
zeroin Avatar asked Jul 02 '16 09:07

zeroin


People also ask

How do I import multiple files into TypeScript?

You could concatenate all those files. That way you only have to import a single module. Alternatively you could list all files in a directory fs. readdir('sources') and then require() then in a for each loop.

How do I import all files into a directory in TypeScript?

To import all modules from a directory in TypeScript, we can create a module that reexports all modules that were imported. export { default as A } from "./a"; export { default as B } from "./b"; to import the default exports from modules a and b .

What is module resolution?

Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import { a } from "moduleA" ; in order to check any use of a , the compiler needs to know exactly what it represents, and will need to check its definition moduleA .

What is module in Tsconfig?

Sets the module system for the program. See the Modules reference page for more information. You very likely want "CommonJS" for node projects. Changing module affects moduleResolution which also has a reference page.


1 Answers

Both module resolution strategies that tsc provides don't support such a behavior. What your desired import statement

import * as mylib from "./source/"; 

is actually doing is to perform checks in this order:

1. (does package.json have a typings key? If so, import this file) 2. import * as mylib from "./source/index.ts"; 3. import * as mylib from "./source/index.tsx"; 4. import * as mylib from "./source/index.d.ts"; 

I'm assuming you're using node-style module resolution here, which you probably are since it's the recommended way. Check the typescript docs for more details on how module resolution is done in typescript.

Usually, what you're trying to accomplish is by creating an index.d.ts file, which serves as the entry point from which you're exporting the rest of your modules. I'm using angular2 as an example: Your common angular2 import looks like this:

import { Injectable } from '@angular/core' 

core is just a directory that lives inside the @angular directory. Just like your source directory. However, in the core directory resides a index.d.ts file:

/**  * @module  * @description  * Starting point to import all public core APIs.  */ export * from './src/metadata'; export * from './src/util'; export * from './src/di'; .... 
like image 89
Georg Grab Avatar answered Sep 19 '22 05:09

Georg Grab