Receive new TypeScript tutorials.
The short answer is definitely YES ! but you need some intermediate steps !
There are many scenarios that we need to cover i.e in the browser and in a node environment .So lets started .
Either create typings for that library but this process can be time consuming and needs a deep knowledge of the library internals .
Or you can just follow these simple steps to include the library without using typings .
First include the library source file before the compiled TypeScript file of your project ,using script tag in your HTML file .
Next ,In your TypeScript file before using the library ,add
declare var libGlobal: any;
You need to replace libGlobal with your library global object which gives access to the library API .
Then just use any library function just like normal .
Using external libraries with TypeScript project for Node environment
Lets suppose you have installed some node library using npm :
npm install --save mylib
If you try to use the import system of TypeScript to import this lib
import * from "mylib";
This will fail with errors such as :
........: error TS2307: Cannot find module 'mylib'.
This can be solved by installing typing for mylib if they do exist .
In case typings for your li don't exit you'll try to use the usual require style of Node
var mylib = require("mylib");
This also will fail with error :
.......: error TS2304: Cannot find name 'require'.
Which means TypeScript can not recognize require statement .But this can be easily solved by installing the typings for node ,which are available from npm ,using :
npm install @types/node --save
About the Author
Read our other tutorials
- Angular Tutorial
- Laravel 6 Tutorial
- React Tutorial
- React Hooks Tutorial
- React Native Tutorial
- Webpack Tutorial
- PHP Tutorial
- Django Tutorial
- Java Spring Tutorial
- Rails 6 Tutorial
- Ionic 4 Tutorial
- TypeScript Tutorial
- Bootstrap 4 Tutorial
- Electron Tutorial
- Cordova Tutorial
- HTML Tutorial
- GraphQL Tutorial
- CSS Grid Layout Tutorial
- PWA Tutorial