Rui Vieira
Rui Vieira

Reputation: 5328

How to import a namespace with using 'require'?

In some code I'm working on I have a mixture of Coffeescript, Javascript and Typescript files.

In my Typescript file, I access the type definitions using

/// <reference path="foo.d.ts" />
import {FooA, FooB} from "foo";

var a:FooA = new FooA() // etc...

This results in the Javascript

var foo_1 = require("foo");
var a = new foo_1.FooA();

Which is fine. However, my workflow is such that I don't use a module system. All the non-JS files are handled by Grunt and compiled to JS and then bundled together.

Ideally I would want my Typescript to compile to

var a = new FooA();

as I can guarantee they would be in the global namespace. How can I do this?

Upvotes: 0

Views: 105

Answers (1)

basarat
basarat

Reputation: 275997

Ideally I would want my Typescript to compile to ... as I can guarantee they would be in the global namespace. How can I do this?

I am sure you know that global is bad and susceptible to subtle ordering issues (beyond just name collision) : some notes

That said. A file will become a module as soon as you have a root level import or export (some docs).

So :

/// <reference path="foo.d.ts" />
import {FooA, FooB} from "foo";

var a:FooA = new FooA() // etc...

Should be something like:

/// <reference path="foo.d.ts" />
var a:FooA = new FooA() // etc...

i.e. FooA should be available globally. You need to review your foo.d.ts. This will help: https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html

Upvotes: 2

Related Questions