How to use typescript modules with a browser?

I am new to typescript and now I am using CJS w / help from a browser. When I added typescript to the mix that the TSC compiler complains about, I have to say

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; 
+6
source share
2 answers

Thanks @AlexB for his answer. Adding def to the top of my app.ts as below solves this problem

 ///<reference path='../typings/node.d.ts' /> var Hello:any = require('hello.js').Hello; 

Update

It is also worth mentioning ... if you do not want / do not want to have type information, you can omit it as follows

 declare var require:any; var Hello:any = require('hello.js').Hello; 
+4
source

You can fix this for any class by making declarations that tell Typescript how to parse the outer class.

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

Include this as a link in your file.

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

+7
source

Source: https://habr.com/ru/post/970096/


All Articles