Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include ambient module declarations inside another ambient module?

Tags:

typescript

I have an ambient .d.ts module which directly depends on Immutable:

/// <reference path="../node_modules/immutable/dist/immutable.d.ts" />
import I = require('immutable');

declare module 'morearty' {
}

But referencing the immutable directly is prohibited by the compiler with this error:

error TS2435: Ambient external modules cannot be nested in other modules.

How could I include the Immutable ambient declarations inside my ambient module? I tried to import immutable from another proxy module but with no luck.

like image 327
bme Avatar asked Nov 16 '14 08:11

bme


People also ask

What is ambient declaration file?

Ambient declarations are a way of telling the TypeScript compiler that the actual source code exists elsewhere. When you are consuming a bunch of third party js libraries like jquery/angularjs/nodejs you can't rewrite it in TypeScript.

What are ambient modules TypeScript?

Ambient modules is a TypeScript feature that allows importing libraries written in JavaScript and using them seamlessly and safely as if they were written in TypeScript. An ambient declaration file is a file that describes the module's type but doesn't contain its implementation.

What is declare module in TypeScript?

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

How do I create a TypeScript module?

A module can be created using the keyword export and a module can be used in another module using the keyword import . In TypeScript, files containing a top-level export or import are considered modules.


1 Answers

Ambient external modules cannot be nested in other modules.

Using an import or export at the root of a file creates a file module. That explains the error nested module.

Fix: Import inside and not at the root of the file:

/// <reference path="../node_modules/immutable/dist/immutable.d.ts" />

declare module 'morearty' {
    import I = require('immutable');
}
like image 158
basarat Avatar answered Oct 27 '22 22:10

basarat