Saturday, 15 January 2011

amd - TypeScript declaring namespaced external module? -



amd - TypeScript declaring namespaced external module? -

i've got legacy js modules either namespaced on window, or define'd if page using amd. example:

// foo/bar.js (function (root, factory) { if (typeof define === "function" && define.amd) { define(factory); } else { root.foo = root.foo || {}; root.foo.bar = factory(); } } (this, function () { homecoming { baz: function () { console.log("qux"); } }; }));

we're trying start using typescript on future pages, i'm having problem creating definition files setup external module. ideally in typescript i'd able import bar = require("foo/bar");

after looking @ how jquery.d.ts it, came with:

// foo/bar.d.ts interface foobar { baz: () => void; } declare module foo { export var bar: foobar; } declare module "foo/bar" { export = foo.bar; // error: not find symbol 'bar'. };

but error on export = foo.bar. i'm not declaring external module , working around using <amd-dependency />.

the exported item can declared variable, appropriate type annotation. here illustration (although i'm not sure want export)...

// foo/bar.d.ts interface foobar { baz: () => void; } declare module foo { export var bar: foobar; } declare module "foo/bar" { export = fb; }; declare var fb: foobar;

this results in global variable fb , supports same "thing" beingness imported via "foo/bar".

if aren't aiming have item in global scope, can drop variable within module, this:

// foo/bar.d.ts interface foobar { baz: () => void; } declare module foo { export var bar: foobar; } declare module "foo/bar" { var fb: foobar; export = fb; }

example calling code...

import foo = require("foo/bar"); var x = foo.baz();

typescript amd

No comments:

Post a Comment