JavaScript Libraries In A TypeScript Application, Revisited
If you haven’t already gotten involved with it, you’ll probably know that TypeScript is becoming increasingly popular. Being able to use a superset of javascript in a typed language that 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 sometimes it can be a real pain. Often JavaScript libraries do not ship with type definitions which are critical when it comes to playing nice with TypeScript.
If you’ve been keeping up with The Polyglot Developer you’ll probably remember two posts that were created. Previously I had written about including external JavaScript libraries in an Angular application as well as adding type definitions to external JavaScript libraries in TypeScript .
We’re going to revisit these two articles and explore all the ways to include JavaScript libraries in TypeScript applications. 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 will handle base64 encoding and decoding without having to worry about atob and btoa . You have to remember that this library is a JavaScript library and was not built with TypeScript in mind.
Several different project examples will be created in an effort to keep things easy to understand.
Create a New Vanilla TypeScript Project
Thefirst project we create will use vanilla TypeScript and will be ran through Node.js. This means we won’t be using Angular, HTML, or anything else. We will 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 two 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 will add the base-64 library and its utf8 dependency. As of right now we have 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 is not something we want to see. So what if we altered the library imports to look like the following instead?:
var base64 = require("base-64"); var utf8 = require("utf8");
The above lines are what 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 are different errors, but still errors nonetheless. This is because the require function is a JavaScript thing, not a TypeScript thing.
So what do we do if we want to resolve these errors and use our library?
One solution would be to download type definitions for each of our libraries if they exist. An 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 compatible.
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 relevant type definition files.
Using a Browser JavaScript Library in a TypeScript Project
If you’re developing web applications, you might run into a situation where you include a JavaScript library via a <script> tag that doesn’t play nice 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 will need to handle TypeScript. Execute the following:
tsc --init
We’ll also need an app.ts and an 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 you with a base64.js and utf8.js file in your project.
JavaScript Libraries In A TypeScript Application, Revisited的更多相关文章
- jsDelivr - Free open source CDN for javascript libraries, jQuery plugins, CSS frameworks, Fonts and more
jsDelivr - Free open source CDN for javascript libraries, jQuery plugins, CSS frameworks, Fonts and ...
- 【HANA系列】SAP HANA XS使用服务器JavaScript Libraries详解
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA XS使用服务器 ...
- 【HANA系列】【第四篇】SAP HANA XS使用服务器JavaScript Libraries详解
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列][第四篇]SAP HANA XS ...
- [译] TypeScript入门指南(JavaScript的超集)
你是否听过 TypeScript? TypeScript 是 JavaScript 的超集,TypeScript结合了类型检查和静态分析,显式接口.TypeScript是微软的开源项目,它是由C#之父 ...
- TypeScript入门指南(JavaScript的超集)
TypeScript入门指南(JavaScript的超集) 你是否听过 TypeScript? TypeScript 是 JavaScript 的超集,TypeScript结合了类型检查和静态分析 ...
- JavaScript 和 TypeScript 交叉口 —— 类型定义文件(*.d.ts)
在 <从 JavaScript 到 TypeScript 系列> 文章我们已经学习了 TypeScript 相关的知识. TypeScript 的核心在于静态类型,我们在编写 TS 的时候 ...
- 使用TypeScript如何提升JavaScript编程效果?
TypeScript是个什么鬼?和JavaScript有什么关系? TypeScript是由微软开发的一种可快速入门的开源的编程语言,是JavaScript的一个超集,且向这个语言添加了可选的静态类型 ...
- [转]Running JavaScript in an iOS application with JavaScriptCore
原文:https://www.infinum.co/the-capsized-eight/articles/running-javascript-in-an-ios-application-with- ...
- TypeScript VS JavaScript 深度对比
TypeScript 和 JavaScript 是目前项目开发中较为流行的两种脚本语言,我们已经熟知 TypeScript 是 JavaScript 的一个超集,但是 TypeScript 与 Jav ...
随机推荐
- github上创建java项目简单操作
github上创建java项目简单操作 参考L: github上创建java项目简单操作 - CSDN博客http://blog.csdn.net/qq_29392425/article/detail ...
- 深入浅出 Java Concurrency (28): 线程池 part 1 简介[转]
从这一节开始正式进入线程池的部分.其实整个体系已经拖了很长的时间,因此后面的章节会加快速度,甚至只是一个半成品或者简单化,以后有时间的慢慢补充.完善. 其实线程池是并发包里面很重要的一部分,在实际情况 ...
- uploadify附件上传 传参
首先 在刚加载jsp时就加入上传方法,所以 formDate 中的参数 zFileName是页面刚加载时 exp1的值 ,后来通过js方法赋值不被读过来,如果 你想要获得这个值,可在 调用upload ...
- python3-常用模块之os
os模块,os是和操作系统交互的模块 os.getcwd() :获取当前工作目录,即当前python脚本工作的目录路径,如果是命令行模式下,同样表示当前目录下 os.listdir(路径): 列出指定 ...
- docker中使用源码方式搭建SRS流媒体服务
一.背景 搭建流媒体服务的方式一般会采用nginx+rtmp和srs服务两种,前者是nginx加上插件所用,而后者是专门为了为了流媒体而生,在这一节中我们将从头搭建srs流媒体服务 二. 运行环境 为 ...
- vue 全局方法(单个和多个方法)
参考: https://www.cnblogs.com/zhcBlog/p/9892883.html https://blog.csdn.net/xuerwang/article/d ...
- golang Linux下编译环境搭建
1.下载golang1.4和1.10源码(1.4以后的版本都用1.4go编译安装,所以先安装1.4) 2.解压后我的目录结构是: /opt/xxx/golang |-------gopath ...
- Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2
给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null,nul ...
- Leetcode240. Search a 2D Matrix II搜索二维矩阵2
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 matrix ...
- 关于dictionary和tuple充当函数参数
需要接收dict时,使用 **name: 需要接收tuple时,使用 *name: --> *name参数后面的任何数据会被认为是’keyword-only’,即它们只能被当作关键词而非参数使用 ...