Vadorequest
Vadorequest

Reputation: 17999

Inheritance TypeScript with exported class and modules

I'm getting crazy with inheritance using typescript. I don't know why but it cannot resolve the class I want to inherit.

lib/classes/Message.class.ts

///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>

export module SharedCommunication {
    export class Message{
         // Stuff
    }
}

lib/classes/ValidatorMessage.class.ts

///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
///<reference path='Message.class.ts'/>

export module SharedCommunication { 
    export class ValidatorMessage extends Message{
        private  _errors;
    }    
}

Message cannot be resolved. I tried SharedCommunication.Message too but it's the same. I reference the class so I don't understand at all what's going on. Do you have any idea?

I tried without the module (two class without be in any module) but it's the same. I need to export the class (and the module if I use it) to get them from another node_module: typescript.api, which I use to load the class and use it in node.

lib/message.js

var Message = require('./classes/Message.class.ts');

module.exports = Message.SharedCommunication.Message;

What's the trick here? Because I have source code on the same project in a different folder working with inheritance, without module or export. Thanks.

Upvotes: 4

Views: 4489

Answers (2)

Akiva Meir
Akiva Meir

Reputation: 11

Take out the export from the mododule declaration:

module SharedCommunication 
{ 
    export class ValidatorMessage extends message.SharedCommunication.Message 
    {
        private  _errors;
    }
}

Upvotes: 1

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220894

ValidatorMessage.class.ts should look like this:

///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>

import message = require('./Message.class');

export module SharedCommunication { 
    export class ValidatorMessage extends message.SharedCommunication.Message {
        private  _errors;
    }
}

It's usually redundant to have a single export module at the top level of a file since the file itself constitutes a namespace anyway.

Bill mentioned this in your other question, but I'd again caution on using RequireTS if you're just starting out with TypeScript - it sounds pretty unmature and is likely to introduce a lot of confusion.

Upvotes: 4

Related Questions