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.
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.
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.
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).
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.
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With