0

This should all work, but doesn't. I have the following typescript code:

module LayoutEngine {
    class WorkerApi {
        private worker : Worker;

        constructor() {
            debugger;
            this.worker = new Worker("layout-worker.js");
        }

And I am trying to create this object in javascript:

require(["../common/events", "worker-api.js"], function (events) {
    var api = new LayoutEngine.WorkerApi();

But I get Uncaught TypeError: undefined is not a function and in the debugger LayoutEngine does evaluate as an object but LayoutEngine.WorkerApi resolves as undefined.

What am I missing in my declaration to create it?

admdrew
  • 3,790
  • 4
  • 27
  • 39
David Thielen
  • 28,723
  • 34
  • 119
  • 193

1 Answers1

0

Are you exporting the classes correctly?

I think you need to "export" the class WorkerApi

So your code should be

module LayoutEngine {
    export class WorkerApi {
        private worker : Worker;

        constructor() {
            debugger;
            this.worker = new Worker("layout-worker.js");
        }

And then, import them like this

var workerApi = require(["../common/events", "worker-api.js"];
var api = new workerApi();

Hope this helps!

Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63
  • I tried that - and then LayoutEngine is undefined also. So even worse. – David Thielen Apr 14 '14 at 20:51
  • Did you reference the files at the top? Something like this-- /// – Cute_Ninja Apr 14 '14 at 21:20
  • No but reference is a .ts construct, I'm having the problem in the .js file. – David Thielen Apr 14 '14 at 21:22
  • Also, you mentioned that you are using typescript but you have '.js' instead of '.ts' in your reference. I am confused :P – Cute_Ninja Apr 14 '14 at 21:23
  • 1
    Aahh.. Got ya.. I think .. See this - http://stackoverflow.com/questions/13753311/load-js-file-into-typescript-file – Cute_Ninja Apr 14 '14 at 21:26
  • No, my problem is the other direction. That's call a javascript object in a typescript file. I need to instantiate a typescript class in a javascript file. – David Thielen Apr 14 '14 at 21:33
  • Ohh, in that case, the only thing i can think of is you should change this line -- class WorkerApi { to this -- export class WorkerApi { ...... i.e. Add export keyword infront of your WorkerApi class, and then it should not give the undefined error. Do not export the module, just the class! – Cute_Ninja Apr 14 '14 at 21:37
  • That was it! I'll mark this as the solution but can you update your answer? Thank You! – David Thielen Apr 14 '14 at 21:53