0

Sample code : Index.ts file which has reference of sample.ts

/// <reference path="sample.ts" />
var s: sample.Calculate = new sample.Calculate(5, 5); -- error thrown
s.alertme();

Sample.ts file :

    module sample {
     export class Calculate {
        constructor(public x: number, public y: number) {
            console.log(x + y);
        }

        alertme() {
            alert('ok');
        }
    }
}

Getting following error where i am calling calculate function : enter image description here

PS: I am using visual studio 2015. HTML:

    @{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    <h1>MVC</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

<div class="row">
    <div class="col-md-4">

    </div>
</div>
@section scripts{    
    <script src="~/Scripts/typings/Index.js"></script>
}
4
  • How are you running this? In node or browser? Commented Sep 3, 2016 at 11:04
  • in browser by running it in debug mode Commented Sep 3, 2016 at 11:07
  • Seems like you're not using a module loader, can you add your html to the question? Commented Sep 3, 2016 at 11:10
  • added!!! i think as i am referencing the ts file module loader is not required Commented Sep 3, 2016 at 11:16

1 Answer 1

1

It seems that your Sample.js file isn't loaded to the browser and because of that the sample module isn't loaded.

Based on your code it seems that you want to load the scripts using script tags and not using a module system, if that's the case then all you need to do is:

@section scripts{
    <script src="~/Scripts/typings/Sample.js"></script>
    <script src="~/Scripts/typings/Index.js"></script>
}

You did tag the question with requirejs so if you do mean to use that then you'll need to do:

// Sample.ts 
export module sample {
    ...
}

// Index.ts
/// <reference path="sample.ts" />
import sample = require('./Sample');
var s: sample.Calculate = new sample.Calculate(5, 5); // should be fine

You'll also need to add the "amd" module to your compiler options.
And you can read more about it in the Modules section of the docs.

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

9 Comments

if i load the script in html itself then /// <reference path="sample.ts" /> is not required
It is required. The /// <reference tags are not for actually loading the scripts, it's only for the compiler to use in order to find the definitions which are used, it's for compilation time only.
Oh. Well, yeah, since it's your file and the .ts file is there and not just the compiled .js. But if you'll try the same with a library you're using which lacks a .ts file then the reference is needed.
Sure, why not? go for it
i am importing module (AMD) : import sample = require("./Sample") In html i am referencing require js as : <script src="~/Scripts/require.js" data-main="~/Scripts/typings/Index.js"></script>
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.