[TypeScript] 1. Catching JavaScript Mistakes with TypeScript
The TypeScript compiler is a powerful tool which catches mistakes even in vanilla JavaScript. Try it online at the TypeScript Playground, zero setup required.
Error version:
var movie = { title: "Memento", year: 2000, IMDB: 8.5, title: "" };
var rating = movie.imdb; function Point(x, x) {
this.x = x;
this.y = y;
}
Point.prototype.distance = function() {
return Math.sqrt(x * x + y * y);
}; function isPast(date) {
var now = Date().getTime();
return date.getTime() < now;
} function ask(question) {
var answer = confirm(question);
var answerView = document.getElementByID('answer');
answerView.innerHTML = answer;
} function handleLoad() { console.log('loaded'); }
document.onLoad = handleLoad(); function newCoinToss() {
return Math.random > 0.5 ? 'HEADS' : 'TAILS';
}
var tosses = [1,2,3].map(newCoinToss); var allHeads = tosses.every(function(toss) {
return toss = 'HEADS';
});
if (allHeads) console.log(allHeads.length, 'heads in a row!'); document.addEventListener('keydown', function(event) {
console.log(event.clientX, event.clientY);
});
Using WebStorm, in the Terminal, it displays the error message for you.
Fixed version in TypeScript:
var movie = { title: "Memento", year: 2000, IMDB: 8.5 };
var rating = movie.IMDB; function Point(x,y) {
this.x = x;
this.y = y;
}
Point.prototype.distance = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}; function isPast(date) {
var now = new Date().getTime();
return date.getTime() < now;
} function ask(question) {
var answer = prompt(question);
var answerView = document.getElementById('answer');
answerView.innerHTML = answer;
} function handleLoad() { console.log('loaded'); }
document.onload = handleLoad; function newCoinToss() {
return Math.random() > 0.5 ? 'HEADS' : 'TAILS';
}
var tosses = [1,2,3].map(newCoinToss); var allHeads = tosses.every(function(toss) {
return toss == 'HEADS';
});
if (allHeads) console.log(tosses.length, 'heads in a row!'); document.addEventListener('mousedown', function(event) {
console.log(event.clientX, event.clientY);
});
[TypeScript] 1. Catching JavaScript Mistakes with TypeScript的更多相关文章
- 玩转TypeScript(引言&文章目录) --初看TypeScript.
JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...
- 使用TypeScript如何提升JavaScript编程效果?
TypeScript是个什么鬼?和JavaScript有什么关系? TypeScript是由微软开发的一种可快速入门的开源的编程语言,是JavaScript的一个超集,且向这个语言添加了可选的静态类型 ...
- 使用Typescript来写javascript
使用Typescript来写javascript 前几天尝试使用haxejs来写javascript,以获得静态类型带来的益处.虽然成功了,但很快发现将它与angularjs一起使用,有一些不太顺畅的 ...
- eval5: TypeScript编写的JavaScript解释器
eval5是基于TypeScript编写的JavaScript解释器,100%支持ES5语法. 项目地址:https://github.com/bplok20010/eval5 使用场景 浏览器环境中 ...
- electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...
- 分享:使用 TypeScript 编写的 JavaScript 游戏代码
<上篇博客>我写出了我一直期望的 JavaScript 大型程序的开发模式,以及 TS(TypeScript) 的一些优势.博客完成之后,我又花了一天时间试用 TS,用它来重构之前编写的一 ...
- CoffeeScript?TypeScript?还是JavaScript
请注意本文只是我的偏见,我努力地理解借助CoffeeScript或TypeScript之类的编译器写JavaScript代码的理由.静态编译.强类型语言和框架,我有着这些流行的.丰富的背景.我的上一份 ...
- 在TypeScript中扩展JavaScript基础对象的功能
最近工作中用到,记录一下:假设我们需要一个功能,把一个数字比如10000输出为下面的字符串格式“10,000”,一般是写一个方法,那么我希望更方便一点,直接向Number类型添加一个格式化方法,比如叫 ...
- [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript
Sometimes we might want to make a function more generic by having it accept a union of different typ ...
随机推荐
- HDU 1400 (POJ 2411 ZOJ 1100)Mondriaan's Dream(DP + 状态压缩)
Mondriaan's Dream Problem Description Squares and rectangles fascinated the famous Dutch painter Pie ...
- Google的代码风格规范,各种语言都很全
https://code.google.com/p/google-styleguide/
- (转载)NET流操作
http://www.oseye.net/user/kevin/blog/86 概念 数据流(Stream)是对串行传输数据的一种抽象表示,是对输入/输出的一种抽象.数据有来源和目的地,衔接两者的就是 ...
- LinearLayout按下(pressed)或获取焦点(focused)时背景设置不同颜色或图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id=&qu ...
- linux下查看防火墙当前状态,开启关闭等
从配置菜单关闭防火墙是不起作用的,索性在安装的时候就不要装防火墙 查看防火墙状态: /etc/init.d/iptables status 暂时关闭防火墙: /etc/init.d/iptables ...
- insmod: error inserting 'simp-blkdev.ko': -1 Invalid module format
(一) 今天写了个块设备驱动例子,在虚拟机上加载模块insmod simp_blkdev.ko的时候,出现以下错误. insmod: error inserting 'simple-blk.ko': ...
- UpdatePanel 无刷新弹出窗口
UpdatePanel下解决提示框不弹出的方法 用户体验上既想页面不刷新,也希望同时能够看到操作的效果(弹出提示框)! ①不刷新,我们可以使用UpdatePanel ②弹出消息框,这个有很多的方式:我 ...
- 使用OLEDB读取不同版本Excel数据的连接字符串设置
摘要: 用OLEDB通过设置连接字符串可以像读取sqlserver一样将excel中的数据读取出来,但是excel2003和excel2007/2010的连接字符串是不同的/// summary // ...
- Oracle将英文字符集数据转换成中文
转换背景:老系统数据为英文字符集,需要将老数据(Oracle 8i)转换到oracle 10g(中文字符集)中 思路:先将老数据从8i的数据库中导出,导出的数据库文件为英文字符集,再将10g的数据库改 ...
- 从微信推送看Android Service的创建和销毁
启动服务是有两组参数影响服务的状态. 1.在onStartCommand(Intent intent, int flags, int startId) 接口中返回值,例如 START_STICKY; ...