Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Typescript support existing projects with JavaScript libraries?

Tags:

typescript

I would like to start using TypeScript in an existing project, but there are already a number of shared JavaScript libraries that I need to use in a TypeScript file. Is there any way for TypeScript to refer to those existing JavaScript libraries with project-related code?

I also tried porting the existing JavaScript libraries to TypeScript and I am getting a lot of compile time errors. It's tough to remove all the compile time errors. Why can't TypeScript just refer those JavaScript classes with a "suppress error and warning" option like Google Dart compiler?

like image 212
vish.Net Avatar asked May 31 '13 21:05

vish.Net


2 Answers

Thanks for all your comments. I got the perfect answer in the below link

How to slowly move to / migrate to TypeScript in an existing JavaScript system

like image 33
vish.Net Avatar answered Nov 16 '22 00:11

vish.Net


There are two cases here:

  1. You are calling JavaScript from TypeScript

In this regard TypeScript works much like the google closure compiler. Everything needs to be declared before it can be used.

So if you have a separate JavaScript file that has a variable Foo (class, module, number etc), you need to tell TypeScript about it. At its most basic case, you can do something like:

declare var Foo: any;

Otherwise you will get compile errors.

Later on you can build on this declaration. For third party libraries there is a huge resource available at https://github.com/DefinitelyTyped/DefinitelyTyped

  1. Copying your JavaScript into TypeScript

An additional thing that might give compile errors is copying over your JavaScript to your TypeScript files. Here TypeScript will help you capture type errors like:

var x = '123';
var y = x.toPrecision(4); // Error x is string not a number 
like image 63
basarat Avatar answered Nov 16 '22 00:11

basarat