Can Typescript consume Existing AMD's?
-
14-07-2021 - |
Question
Dojo 1.8 already defines AMD modules. For example you can do things like this:
require(["dojo/_base/lang"], function (lang) {
var ab = lang.mixin({a: 1}, {b: 2});
});
But how to I avoid getting an error when I attempt to import this module?
import lang = module ("dojo/_base/lang");
Is is possible?
Solution
You will need a typescript definition file for lang. Assuming that lang.d.ts
exists in the same directory as lang.js
this code:
import lang = module('dojo/_base/lang')
var ab = lang.mixin({a: 1}, {b: 2});
compiled with
tsc --module amd yourfile.ts
generates
define(["require", "exports", 'dojo/_base/lang'], function(require, exports, __lang__) {
var lang = __lang__;
var ab = lang.mixin({a: 1}, {b: 2});
}
If you don't want to have to match up the directory structures for whatever reason do this instead. Assuming lang.d.ts
is in in a subdirectory called 3rd
that is a sibling of test.ts
.
test.ts:
///<reference path="3rd/lang.d.ts"/>
import lang = module('dojo/_base/lang');
var ab = lang.mixin({a: 1}, {b: 2});
3rd/lang.d.ts:
declare module 'dojo/_base/lang' {
...
}
generates the same as above.
OTHER TIPS
You can normally load modules via the dojo loader, you don't have to use the import statement. But if you want you have to declare the module:
declare module "dojo/_base/lang" {
export function ...
export class ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow