Call JavaScript functions from TypeScript

What is the best way to import and use JavaScript code in TypeScript?

I am trying to use things like Bootstrap modal.js in 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 the cases I needed to use, and then the executable js file implementation was used at runtime.

It is a shame that there is no more automatic direct route, but it does its job. For many common frameworks, these d.ts declarations are available as indicated through http://definitelytyped.org/tsd . I also did not have to comment on the d.ts style link, as it was already included in my compiled ts files.

+4
source share
1 answer

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

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

You can then reference the definition in Typescript as follows:

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

In general, you mainly refer to the library, then in Typescript you must refer to the type definition or provide your own declaration, for example

declare function hex_md5(value: string): string;

if you have a javascript function hex_md5defined somewhere above.

+6
source

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


All Articles