4

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.

2 Answers 2

4

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.

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

4 Comments

Yes, exactly! I found it few minutes before your answer, but I still don't understand why the reference is useless here. I don't really understand your point of view about export module. I need to export it to get access to it somewhere else. I use typescript.api as TypeScript file loader, works well. I need it, and it works unlike require-typescript.
I'm trying to write up some documentation on this. It's admittedly confusing. The samples included with the TypeScript git repo do a good job showing some of the normal patterns for program structure.
Yeah but my friend who are used to use TS and has already wrote a lot of script with it don't use the import but only references and it works well ^^ Thank you for your help, I'm looking forward about TS documentation.
I have another problem with this source code. I want to use it on the browser now but it's not working because of the call to require(). Require is undefined. I tried to write it: require = function(file){ var files = file.split('/'); file = files[files.length - 1].replace('.class', ''); return exports[file] || window[file]; }
1

Take out the export from the mododule declaration:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.