mat
mat

Reputation: 1378

TypeScript dynamic loading of AMD module ends in "Could not find symbol '...'

No, this topic won't answer my question and NO, the solution is not simply importing Command in the nav.ts file. nav.ts is one of many viewModel-files and they will be loaded dynamically on demand. The only problem is to set the parameter's type in the constructor of the class. (Type has to be "Command")


In the following class, which will be loaded by require.js, the method viewModel() requires a new class dynamically. In this case NavViewModel .

command.ts

export class Command {

...

    public viewModel(name: string, callback: Function) {
        require(["noext!boot/getViewModel/" + name], function (viewModel) {
            callback(viewModel);
        });
    }
}

This is the class which will be fetched by viewModel():

nav.ts

export class NavViewModel extends kendo.Router {
    constructor(command: Command) {
        super();

        this.route('/:name', function (name) {
            command.view(name, $('div.content'));
        });

        this.start();
    }
}

EDIT: Here is the entry-point (requested in comment 2)

main.ts (EntryPoint)

import lib = require("command");

var cmd = new lib.Command();
cmd.viewModel('nav', function (o) {
    cmd.view('nav', $('div.header'), function () {
        kendo.bind($('.header .nav'), new o.NavViewModel(cmd));
    });
});

/EDIT

The Problem:

Visual Studio will throw the error TS2095: Could not find symbol 'Command', because the "Command" class ist not defined in this Module.

The program works fine if the "Command"-Type will be removed from the NavViewModel constructor. Is there any solution to reference the Command class in the NavViewModel?

This won't work:

/// <reference path="../../Scripts/command.ts" />

Upvotes: 1

Views: 1461

Answers (1)

Robert Slaney
Robert Slaney

Reputation: 3722

When using RequireJS, the import statement should be the full path from the root of the application.

I also use a slightly different export syntax

command.ts

class command {
    ...
}

export = command;

main.ts

// I'm assuming the Scripts folder is at the root of the application
import Command = require('Scripts/command');

var cmd = new Command();

Note

I'm using Typescript 0.9.1.1. I can't upgrade my machine to 0.9.5 as a large internal application is affected by some breaking changes between versions

Upvotes: 1

Related Questions