JavaScript Libraries in A TypeScript application, revisited

Source: Internet
Author: User

If you haven ' t already gotten involved with it, you'll probably know that TypeScript is becoming increasingly. Being able to use a superset the JavaScript in a typed language the compiles down to JavaScript is a great thing. However,if you ' ve ever played around with TypeScript and tried to use JavaScript libraries, you'll probably know that some Times it can be a real pain. Often JavaScript libraries don't ship with type definitions which is critical when it comes to playing nice with TYPESCR Ipt.

If you've been keeping up and the Polyglot Developer you'll probably remember the posts that were created. Previously I had written about including external JavaScript libraries in a Angular application as well as adding Type de Finitions to external JavaScript libraries in TypeScript.

We ' re going to revisit these both articles and explore all the ways to include JavaScript libraries in TypeScript Applicati Ons. These include applications built with Nativescript, Ionic, and Angular.

We ' re going to use a particular JavaScript library for the examples used throughout this particular article. This library is called base-64 and would handle Base64 encoding and decoding without have to worry about Atob and Btoa. You have to remember the This library are a JavaScript library and was not the built with the TypeScript in mind.

Several different project examples'll be created on an effort to keep things easy to understand.

Create a New Vanilla TypeScript Project

Thefirst Project we create would use vanilla TypeScript and would be the RAN through node. js. This means we won ' t is using Angular, HTML, or anything else. We'll create a TypeScript file, compile it, and run it via node. js.

Create a new project directory and execute the following commands:

Npminit--y TSC--init

The above commands should leave us with a Package.json file as well as a Tsconfig.json file. The next step is to add the libraries we wish to use via the Node package Manager (NPM):

npminstallbase-64 UTF8--save

The above command would add the BASE-64 library and its UTF8 dependency. As of right now we had a few JavaScript dependencies and our project, but no source code to our application.

Create and open an app.ts file and add the following TypeScript code:

Import * as Base64from "base-64"; Import * as Utf8from "UTF8"; var str = "Nicraboy"; var bytes = Utf8.encode (str); var encodedstr = base64.encode (bytes); Console.log (ENCODEDSTR);

The above code was taken from the library documentation. However, if you try to compile this file by executing TSC, you'll end up with errors that look like the following:

App.ts (1,25): Error ts2307:cannot find module ' base-64 '. App.ts (2,23): Error ts2307:cannot find module ' UTF8 '.

The above isn't something we want to see. So, if we altered the library imports to look like the following instead?:

var base64 = require ("base-64"); var UTF8 = require ("UTF8");

The above lines is what do you ' d typically see in a JavaScript application, not necessarily a TypeScript application. When we try to compile our application with the Require statements, we end up with the following messages:

App.ts (1,14): Error ts2304:cannot find name ' require '. App.ts (2,12): Error ts2304:cannot find name ' require '.

The above lines is different errors, but still errors nonetheless. This is because the Require function was a JavaScript thing, not a TypeScript thing.

So what does we do if we want to resolve these errors and use our library?

One solution would is to download type definitions for each of our libraries if they exist. A example of this would look like the following:

Npminstall @types/base-64 @types/utf8--save

If We download the type definitions we can continue to use the import statements that we saw previously. Execute the TSC command and it should compile without issues. You can test the project by executing the following:

Nodeapp.js

However, what happens if the type definitions don ' t exist or what if we ' d like to use the Require commands instead? We can actually obtain the type definitions for the Require command so it becomes TypeScript.

Install the following type definitions to use the Require command:

Npminstall @types/node--save

With the node type definitions we can use any JavaScript library in our TypeScript project without needing to find the Rel Evant type definition files.

Using a Browser JavaScript Library in a TypeScript Project

If you ' re developing Web applications, you might run to a situation where you include a JavaScript library via a <SCR ipt> tag that doesn ' t play friendly in your TypeScript code.

Let's create a new project somewhere on our computer. We ' re not going to create a node. js project, but we'll need to handle TypeScript. Execute the following:

TSC--init

We ' ll also need an app.ts and a index.html file in our project. Finally, since this a browser based project, we need to download the base-64 Library and UTF8 library. This should leave is a base64.js and Utf8.js file in your project.

JavaScript Libraries in A TypeScript application, revisited

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.