Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In TypeScript, you can identify if an import refers to a directory or a module by looking for the presence of an index.ts or index.d.ts file within the directory.

By default, when importing a directory in TypeScript, it will look for an index.ts or index.d.ts file within the directory. If found, TypeScript will assume that the import refers to the directory as a whole, and will use the exported members from that file as the default export for the import.

For example, if you have a directory called foo, and it contains an index.ts file with the following code:

export const foo = 'foo';
export const bar = 'bar';

You can import the foo directory in another TypeScript file like this:

import {foo, bar} from './foo';
console.log(foo, bar); // 'foo' 'bar'

Alternatively, if there is no index.ts or index.d.ts file within the directory, TypeScript will assume that the import refers to a module with the same name as the directory, and will look for a module declaration file (.d.ts) or an implementation file (.ts) within the directory with the same name as the imported module.

For example, if you have a directory called foo, and within that directory there is a file called foo.d.ts with the following code:

export const foo: string;
export const bar: number;

You can import the foo module in another TypeScript file like this:

import {foo, bar} from './foo';
console.log(foo, bar); // 'foo' 1

Note that the imported members can have different types based on the definitions in the module declaration file.