Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: MyClass is not defined #52

Closed
staff0rd opened this issue Jan 9, 2016 · 3 comments
Closed

ReferenceError: MyClass is not defined #52

staff0rd opened this issue Jan 9, 2016 · 3 comments
Labels

Comments

@staff0rd
Copy link

staff0rd commented Jan 9, 2016

I have the following files;

MyClass.ts

class MyClass {
    x: number;
    y: number;
    constructor(x: number, y :number) {
        this.x = x;
        this.y = y;
    }
}

MyScript.ts

/// <reference path="MyClass.ts" />
var myClass: MyClass = new MyClass(2,3);
console.log(myClass.x, myClass.y);

Compiling via tsc is successful, however I cannot execute MyScript.cs with ts-node. Am I doing something wrong?

c:\git\ts-node-test>tsc MyScript.ts

c:\git\ts-node-test>ts-node -v
0.5.5

c:\git\ts-node-test>ts-node MyScript.js

c:\Users\stafford\Documents\git\ts-node-test\MyScript.js:2
var myClass = new MyClass(2, 3);
                  ^
ReferenceError: MyClass is not defined
    at Object.<anonymous> (c:\Users\stafford\Documents\git\ts-node-test\MyScript.js:2:19)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:430:10)
    at Object.<anonymous> (C:\Users\stafford\AppData\Roaming\npm\node_modules\ts-node\src\bin\ts-node.ts:121:12)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)

c:\git\ts-node-test>

Repro

@blakeembrey
Copy link
Member

This is still Node, you need to import or require that file somehow. References in TypeScript are used to tell the compiler this file exists in the current context, but in your case it does not. Try doing:

export class MyClass {
    x: number;
    y: number;
    constructor(x: number, y :number) {
        this.x = x;
        this.y = y;
    }
}

And

import { MyClass } from './MyClass'

var myClass = new MyClass(2,3);
console.log(myClass.x, myClass.y);

staff0rd added a commit to staff0rd/ts-node-test that referenced this issue Jan 9, 2016
@staff0rd
Copy link
Author

staff0rd commented Jan 9, 2016

This of course works, much appreciated.

As to expand on my understanding, are you able to explain why in my case the reference doesn't tell the compiler the file exists? Is there an expectation of tsconfig.json here?

@blakeembrey
Copy link
Member

@staff0rd It tells the TypeScript compiler this file exists within scope fine, that works. The error your getting is from node. Just because the TypeScript compiler believes you when you said it was in scope, it doesn't mean it actually is during runtime. Personally, I avoid references since I'm usually developing for CommonJS output. If you want to develop global browser modules, that's where you might use references with TypeScript (or, for global polyfills and .d.ts files, etc).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants