1

I am having a tough figuring out how to mix RequireJs and TypeScript. I am compiling TypeScript with the AMD option. The problem is, the function declared within an exported class is classified as "prototype". Is this normal?

ModalHelper.ts:

export class ModalHelper {
    saySomething = (something: String) => {
        alert(something);
    }
}

Compiled ModalHelper.js:

define(["require", "exports"], function (require, exports) {
    var ModalHelper = (function () {
        function ModalHelper() {
            this.saySomething = function (something) {
                alert(something);
            };
        }
        return ModalHelper;
    })();
    exports.ModalHelper = ModalHelper;
});

Create.ts and Create.js (module is marked as required):

require(["jquery", "Shared/ModalHelper"], function ($, modal) {
    $(function () {
        modal.ModalHelper.saySomething("hi");
    });
});

Why am I not able to call this function? From debugging, I see the module being added via RequireJs.

2 Answers 2

2

There are two options.

You are calling it like a static method, so you could make it static:

export class ModalHelper {
    static saySomething = (something: String) => {
        alert(something);
    }
}

Or if you didn't mean for it to be static, you can instantiate the ModalHelper first instead:

var helper = new ModalHelper();
helper.saySomething("hi");
Sign up to request clarification or add additional context in comments.

Comments

1

IMO classes in TypeScript should be used only if you need multiple instances with data associated to each instance. A Helper is usually just a bunch of static methods. Therefore I suggest to use a module instead (untested):

Shared/ModalHelper.ts

module ModalHelper {
    export function saySomething(something: String) {
        alert(something);
    }
}

export = ModalHelper

Create.ts

import ModalHelper = require("./Shared/ModalHelper");

module Create {
    ModalHelper.saySomething("Hello");
}

Comments

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.