Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use typescript modules w/ browserify?

I'm new to typescript and I'm currently using CJS w/ help from browserify. When I added typescript to the mix the TSC compiler complains about require saying

error TS2095: Could not find symbol 'require'

This is my entry point for browerify

var Hello:any = require('hello.js').Hello;

here is my hello js file (required above)

var React = require('react');

var Hello = React.createClass({displayName: 'Hello',
    render: function() {
        return React.DOM.div(null, "Hello ", this.props.name);
    }
});

exports.Hello = Hello;
like image 483
Toran Billups Avatar asked May 30 '14 14:05

Toran Billups


2 Answers

You can fix this for any class by making declarations which tell Typescript how to parse an external class.

Fortunately for you, this has already been done for a large number of libraries.

Include this as a reference in your file.

https://github.com/borisyankov/DefinitelyTyped/tree/master/browserify

like image 65
AlexB Avatar answered Oct 26 '22 10:10

AlexB


Thanks to @AlexB for his reply. Adding the def to the top of my app.ts like I show below solved this issue

///<reference path='../typings/node.d.ts' />

var Hello:any = require('hello.js').Hello;

Update

It's also worth a mention ... if you don't want/care to have the typing information you can omit it as follows

declare var require:any;

var Hello:any = require('hello.js').Hello;
like image 43
Toran Billups Avatar answered Oct 26 '22 11:10

Toran Billups