Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

What's the best way of importing and using JavaScript code within TypeScript?

I'm trying to use stuff like Bootstrap's modal.js within an Angular 2 Component written using TypeScript.

UPDATE:

The solution as suggested was to create a type declaration d.ts file for the js file to forward delcare things I needed to use, then the referenced js file implementation was used at runtime.

Shame there isn't a more automatic direct way but it gets the job done. For many common frameworks these d.ts type declarations are available as mentioned via http://definitelytyped.org/tsd. I also didn't need to comment style reference the d.ts as it was already included in my compiled ts files.

share|improve this question
up vote 5 down vote accepted

You can use type declaration or write your own. Take a look on this project http://definitelytyped.org/tsd/

It has type definitions for many popular libraries. And also for angular-ui-bootstrap.

Then you can reference definition in Typescript like this:

///<reference path="../typings/angular-ui-bootstrap/angular-ui-bootstrap.d.ts"/>

In general you basically reference a library, then in typescript you should reference a type definition or provide your own declaration, like

declare function hex_md5(value: string): string;

if you have javascript function hex_md5 defined somewhere above.

share|improve this answer
1  
Alternatively, do not use ///<reference ..., use a tsconfig.json and do not exclude the typings directory containing the typings definition files; they will be referenced by the compiler automatically. – Bruno Grieder Feb 18 at 13:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.