Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Typescript generating javascript that doesn't work

Typescript generating javascript that doesn't work

Problem

Node is not happy about something in the Javascript that TypeScript is generating and I can't figure out why. Here is the code

Person.ts:

export module Models {
    export class Person {
        id: number;
        firstName: string;
        lastName: string;

        constructor(f: string, l: string) {
            this.firstName = f;
            this.lastName = l;
        }

        public saySomething(): void {
            console.log("my name is %s %s", this.firstName, this.lastName);
        }
    }
}

Database.ts:

import P = module("Person");

export module Database {
    export class DB {
        public findPerson(id: number): P.Models.Person {
            return new P.Models.Person("a", "b");
        }
    }
}

Test.ts:

var D = require("./DB");
var db = new D.Database.DB();
var p = db.findPerson(123);
p.saySomething();

It compiles fine down in Javascript, but there appears to be something wrong with the DB.js file that is being created. When I try to run Test.js in node I get this:

$node Test.js
DB.js:2
(function (Database) {
^
TypeError: object is not a function
    at Object. (DB.js:2:1)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object. (Test.js:1:73)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)

It's happening during the import of DB.js. Oddly enough, if I change the findPerson() method to return a string instead of a Person it works fine. It has something to do with creating that Person object.

Here is the Javascript:

(function (Models) {
    var Person = (function () {
        function Person(f, l) {
            this.firstName = f;
            this.lastName = l;
        }
        Person.prototype.saySomething = function () {
            console.log("my name is %s %s", this.firstName, this.lastName);
        };
        return Person;
    })();
    Models.Person = Person;    
})(exports.Models || (exports.Models = {}));


var P = require("./Person")
(function (Database) {
    var DB = (function () {
        function DB() { }
        DB.prototype.findPerson = function (id) {
            return new P.Models.Person("a", "b");
        };
        return DB;
    })();
    Database.DB = DB;    
})(exports.Database || (exports.Database = {}));


var dbx = require("./DB")
var db1 = new dbx.Database.DB();
var p = db1.findPerson(123);
p.saySomething();
Problem courtesy of: d512

Solution

without semicolon after var P = require("./Person") javascript thinks than next (...) is call of a Function. but you seem have it in Database.ts, so Typescript probably loosing some semicolons in compilation.

Solution courtesy of: Dima Vidmich

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Typescript generating javascript that doesn't work

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×