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的更多相关文章

  1. 玩转TypeScript(引言&文章目录) --初看TypeScript.

    JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...

  2. 使用TypeScript如何提升JavaScript编程效果?

    TypeScript是个什么鬼?和JavaScript有什么关系? TypeScript是由微软开发的一种可快速入门的开源的编程语言,是JavaScript的一个超集,且向这个语言添加了可选的静态类型 ...

  3. 使用Typescript来写javascript

    使用Typescript来写javascript 前几天尝试使用haxejs来写javascript,以获得静态类型带来的益处.虽然成功了,但很快发现将它与angularjs一起使用,有一些不太顺畅的 ...

  4. eval5: TypeScript编写的JavaScript解释器

    eval5是基于TypeScript编写的JavaScript解释器,100%支持ES5语法. 项目地址:https://github.com/bplok20010/eval5 使用场景 浏览器环境中 ...

  5. electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint

    我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...

  6. 分享:使用 TypeScript 编写的 JavaScript 游戏代码

    <上篇博客>我写出了我一直期望的 JavaScript 大型程序的开发模式,以及 TS(TypeScript) 的一些优势.博客完成之后,我又花了一天时间试用 TS,用它来重构之前编写的一 ...

  7. CoffeeScript?TypeScript?还是JavaScript

    请注意本文只是我的偏见,我努力地理解借助CoffeeScript或TypeScript之类的编译器写JavaScript代码的理由.静态编译.强类型语言和框架,我有着这些流行的.丰富的背景.我的上一份 ...

  8. 在TypeScript中扩展JavaScript基础对象的功能

    最近工作中用到,记录一下:假设我们需要一个功能,把一个数字比如10000输出为下面的字符串格式“10,000”,一般是写一个方法,那么我希望更方便一点,直接向Number类型添加一个格式化方法,比如叫 ...

  9. [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 ...

随机推荐

  1. 【HDOJ】1297 Children’s Queue

    递推,最近发现自己做递推的题总是没有思路.下周多练习.对于f(n)可以在第n个位置为男生,此时共有f(n-1)种情况:若在第n个位置为女生,因此第n-1个位置也必须为女生.此时有两种情况,一种情况是在 ...

  2. Outlook接收qq的邮件

    1.先去qq邮箱,设置,账户 开启pop3服务,假如之前开启过,最好关闭之后重新开启 最新版本的必须使用邮箱的独立密码才可以收取邮件 (否则就算你之前开通了,也无法用你的qq账号和密码收取邮件的) 2 ...

  3. Linq中SingleOrDefault、FirstOrDefault的用法

    1.SingleOrDefault和FirstOrDefault的区别 SingleOrDefault 只取一个 如果没有数据等于 null, 如果>1  异常 FirstOrDefault   ...

  4. Nagios "process_cgivars()" 单字节溢出漏洞

    漏洞版本: Nagios Nagios 4.x Nagios Nagios 3.x 漏洞描述: Nagios是一款免费开放源代码的主机和服务监视软件,可使用在多种Linux和Unix操作系统下. Na ...

  5. PHP位运算 详细说明

    在实际应用中可以做用户权限的应用我这里说到的权限管理办法是一个普遍采用的方法,主要是使用到”位运行符”操作,& 位与运算符.| 位或运行符.参与运算的如果是10进制数,则会被转换至2进制数参与 ...

  6. LightOJ 1236 Pairs Forming LCM 合数分解

    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...

  7. (转)使用pth文件,让python方便的import自己写的模块

    转自:http://www.elias.cn/Python/PythonPath 文章简单易懂,有理有据. 有时候我们正在修改或调试的程序会是一个库,为修改方便,我们可能不大希望把它放到 site-p ...

  8. Failed to load unit 'PATM' (VERR_SSM_FIELD_NOT_CONSECUTIVE)

    今天打开虚拟机启动的时候报错:Failed to load unit 'PATM' (VERR_SSM_FIELD_NOT_CONSECUTIVE) 后来发现虚机处于休眠状态,所以在虚机上右键,然后清 ...

  9. seg格式文件的分析

    s ,r, c1, c2   第r行的c1列到 c2列的值为s

  10. 判断两棵树是否相等 leecode

    很简单 提交代码 https://oj.leetcode.com/problems/same-tree/ iven two binary trees, write a function to chec ...