0

In my javascript file I have:

require(["menu/main-menu"], function (mainMenu) {

where main-menu is built from main-menu.ts. javascript/requireJS wants me to build up a define I return in main-menu. But typescript doesn't do that AFAIK.

How can I accomplish this?

Update: For example, the following tells me the export keyword is incorrect:

///<reference path='../../libs/ExtJS-4.2.0.d.ts' />
///<reference path='../../libs/require.d.ts' />

import fdm = require("./file-definitions");
require(["../../scripts/ribbon"], function () {

export module Menu {

    export class MainMenu {
    }
}
2
  • "import" is for external modules. Once you use it, the file should be compiled into an external (i.e. RequireJS or CommonJS) module. "module" is for internal modules. I don't think you can export an internal module from an external module. In addition, you need to use the "export =" syntax for RequireJS modules because TypeScript defaults to CommonJS-style modules. Commented Nov 21, 2013 at 8:32
  • In other words, something like: import fdm = require("./file-definitions"); import ribbon = require("../../scripts/ribbon"); class MainMenu { ... } export = MainMenu; Commented Nov 21, 2013 at 8:34

2 Answers 2

1

If I understand your question correctly you want to access MainMenu in JavaScript using RequireJS? Then your JavaScript should look like:

require(["path/to/MainMenu"],function(mainMenu){
    mainMenu.doSomething();
});

Now, assuming you are using TypeScript 0.9.1 or later, you can assign a value to the exported value of your module. So you could write your MainMenu.ts file like this:

class MainMenu {
    doSomething(): void {
    }
}
export = MainMenu;

Edit: You are using the export statement inside of a function which is not allowed. If your code relies on the ribbon script being included, I suggest you require it with an import statement as well. So:

import ribbon = require("../../scripts/ribbon");

Now, if this is an external script and you haven't written it in TypeScript yourself, you need to tell the compiler this module exists. In your declaration file add the following:

declare module "../../scripts/ribbon" {};

A declaration file is a TypeScript file with the extension *.d.ts. It contains only declarations, not implementations. You could compare it to a C header file. For example, you can download a declaration file for jQuery. This will give you the TypeScript compiler support for jQuery without actually needing a TypeScript version of jQuery.

The same principle can be used here, for the ribbon file. If you don't already have a declaration file, simply add one to your project, for example: declarations.d.ts. If you are using Visual Studio as your editor, normally the TypeScript plugin should automatically pick up this declaration file. You can check if the file is correctly picked up in case the import statement for ribbon above does not throw an error. In case this does not work, simply add a reference statement in MainMenu.ts:

///<reference path='../../libs/declarations.d.ts' />

Provided you have created the declarations.d.ts file under the same lib as the other declaration files you were already referencing.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried that, still doesn't work (see the update with my specific code listed). Is there anything else? Thanks - dave
I still get an error on the export in "export module Menu". How can I avoid that? thanks - dave
What version of TypeScript are you using?
I'm using version 0.9.1.1 which I believe is the latest.
I'm using version 0.9.1.1 which I believe is the latest.
|
0

To do a manual require (e.g. for a lazy load) with a callback you can use the RequireJS definitions for TypeScript from DefinitelyTyped : https://github.com/borisyankov/DefinitelyTyped/tree/master/requirejs

3 Comments

How do I set up a manual require? I tried require(...) and can't find a way that works.
Exactly the same as you would do in javascript
I tried that, still doesn't work (see the update above with my specific code listed). Is there anything else? Thanks - dave

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.