Denis Gladkiy
Denis Gladkiy

Reputation: 2174

Import stuff from TypeScripd declaration file with only "declare module" top level declarations

I've got a TypeScript file foo.d.ts. It contains a lot of "declare module" constructs and nothing more:

declare module Foo {
}

declare module Fun {
}

The code is auto-generated by the Bridge.NET, so I can't modify it (I can, actually. But it is not a good-looking solution).

If the file is referenced in another one with import * as bridge from "./foo"; the tsc says that "File foo.d.ts is not a module". What I'm doing wrong?

If I edit the generated file (foo.d.ts) and add to the end of it dummy top-level export (export interface Fix {}) the problem disappears.

Upvotes: 1

Views: 990

Answers (1)

AndreyCh
AndreyCh

Reputation: 1413

The reason of the error you've got is that your foo.d.ts is not actually a module, it's a script. But import statement is supposed to be used with modules only. When you added export interface Fix {}, this transformed the file into a module and that is why the error had gone.

The common rule is pretty simple, TypeScript compiler treats a file as a module if there is at least one top-level entity (class/interface/variable ..) containing export modifier. Otherwise, the file will be processed just like a script.

If you don't really need foo.d.ts to be a module, you can refer it using the directive below. In this case all entities declared in the file will be globally available.

/// <reference path="foo.d.ts" />

Upvotes: 3

Related Questions