Why are non-exported types implicitly exported from a TS declaration file with imports but no exports? Is this a bug? If not, what is this behavior called and is it documented anywhere?

Background:

I understand that in a TS declaration files with no imports or exports every declaration becomes ambient, and is available without imports.

In a declaration file with ECMAScript module exports, types from the file are only available from other files if they are explicitly exported.

There seems to be a third behavior of TS declaration files that have at least one import or export statement but no export declarations: Any variable/function/interface etc. declaration in such a file can be imported from other files, even though they weren't explicitly exported.

Example:

weird.d.ts (imports from self)

import {} from "./weird";
declare const implicitlyExposed: {};

importer.d.ts

import { implicitlyExposed } from "./weird"; // no error, is usable import

Clarification

Note that unexported declarations are only leaked from a .d.ts file that is a module if there are no export declarations. An export declaration looks like this:

declare const foo: number;
export { foo };

TS treats such an export differently from a statement with an export modifier:

export declare const foo: number;

Using the former syntax prevents non-exported declarations from being importable from other files.

0

There are 0 answers