[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 ...
随机推荐
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-004-Pizza例子的用户流程(flowExecutionKey、_eventId_phoneEntered、flowExecutionUrl )
一. 1. 2. 3.customer-flow.xml 自己定义customer,最后output <?xml version="1.0" encoding="U ...
- RxJava开发精要1-从.NET到RxJava
原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...
- Ember.js demo8
<!DOCTYPE html> <html> <head> <meta name="description" content=" ...
- Oracle系列之索引
涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等 Oracle索引.权限 介绍 为什么添加了索引后,会加快查询速度呢? 索引是用于加速数据存取的数据对象.合理的使用索引可以大大降低i/ ...
- SQL Server登录 18456错误
1.以windows验证模式进入数据库管理器. 第二步:右击sa,选择属性: 在常规选项卡中,重新填写密码和确认密码(改成个好记的).把强制实施密码策略去掉. 第三步:点击状态选项卡:勾选授予和启用. ...
- 数组MARSHALLING z
在托管代码和本地代码之间传递数组,是interop marshaling中间比较复杂的一个问题.本文从数组的定义开始,介绍数组marshalling的三种方法,并对blittable类型等概念做进一步 ...
- aggregate 和 treeAggregate 的对比
1.定义 [aggregate] /** * Aggregate the elements of each partition, and then the results for all the pa ...
- Codeforces 439 A. Devu, the Singer and Churu, the Joker
这是本人第一次写代码,难免有点瑕疵还请见谅 A. Devu, the Singer and Churu, the Joker time limit per test 1 second memory l ...
- 【HTML】Advanced5:Accessible Forms
1.label <form> <label for="yourName">Your Name</label> <input name=&q ...
- 【JS】Beginner1:Making Stuff Happen
1.JS(JavaScript) is for interactivity 2.How does JS relate to HTML&CSS? script tag script elemen ...