12

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?

2 Answers 2

10

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 
Sign up to request clarification or add additional context in comments.

Comments

3

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

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.