[TypeScript] Avoid any type
To avoid using "any" type is a best pratice. The reason for that is it disable the power of typescirpt to helping check you during the compile time. For example:
currency(num: number){
cosnole.log(num.toFixed());
}
In our currency function, it should accept a number not a string, because toFixed is a method for number type.
In typescript, it will tell you, if the type is not number, but string, it will report error:

If you cannot sure it is a string or number type, you can use unit type:
currency(num: string|number){
if(typeof num === "number"){
cosnole.log(num.toFixed());
}else{
console.log(parseFloat(num).toFixed());
}
}
[TypeScript] Avoid any type的更多相关文章
- [TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking
TypeScript 2.0 introduced a new primitive type called never, the type of values that never occur. It ...
- [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type
ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...
- [TypeScript] Define Custom Type Guard Functions in TypeScript
One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a ...
- [Typescript] Installing Promise Type Definitions Using the lib Built-In Types
To fix Promise is not recolized in TypeScript, we can choose to use a lib: npm i @types/es6-promise ...
- [Redux] Avoid action type naming conflicts
In redux, the action type is just a normal string type, it is easy to get naming conflicts in large ...
- 【区分】Typescript 中 interface 和 type
在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...
- 10 Essential TypeScript Tips And Tricks For Angular Devs
原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...
- CSS3与页面布局学习总结(七)——前端预处理技术(Less、Sass、CoffeeScript、TypeScript)
CSS不像其它高级语言一样支持算术运算.变量.流程控制与面向对象特性,所以CSS样式较多时会引起一些问题,如修改复杂,冗余,某些别的语言很简单的功能实现不了等.而javascript则是一种半面向对象 ...
- CSS3与页面布局学习笔记(七)——前端预处理技术(Less、Sass、CoffeeScript、TypeScript)
CSS不像其它高级语言一样支持算术运算.变量.流程控制与面向对象特性,所以CSS样式较多时会引起一些问题,如修改复杂,冗余,某些别的语言很简单的功能实现不了等.而javascript则是一种半面向对象 ...
随机推荐
- LINUX增加并管理用户
使用groupadd命令增加新的用户组 使用useradd命令增加新用户: useradd -s /bin/bash -d /home/XXX -g group username 使用passwd修改 ...
- MVC 5 下,应用log4net收集异常信息
1.安装log4net. 首先在VS中通过nuget安装logenet,我用的是VS2013,截屏如下:
- C#多态联系之虚方法
class Class1 { static void Main(string[] args) { YuanGong yg = new YuanGong(); JingLi jl = new JingL ...
- 最近公共祖先:LCA及其用倍增实现 +POJ1986
Q:为什么我在有些地方看到的是最小公共祖先? A:最小公共祖先是LCA(Least Common Ancestor)的英文直译,最小公共祖先与最近公共祖先只是叫法不同. Q:什么是最近公共祖先(LCA ...
- Java开发环境安装
一.安装JDK(java development kit) 下载地址:www.oracle.com/technetwork/java/javase/downloads 二.配置Java环境变量 1.J ...
- [GIT] warning: LF will be replaced by CRLF问题解决方法
原文链接[http://michael-roshen.iteye.com/blog/1328142] 开发环境: 操作系统: windows xp ruby 1.9.2 rails 3.1.3 git ...
- JQuery日历插件My97DatePicker日期范围限制
My97DatePicker是一个非常优秀的日历插件,不仅支持多种调用模式,还支持日期范围限制. 常规的调用比较简单,如下所示: 1 <input class="Wdate" ...
- 使用PHP解压文件Unzip
这是一个非常方便的PHP函数从.zip文件解压缩文件.它有两个参数:第一个是压缩文件的路径和第二 function unzip_file($file, $destination) { // creat ...
- python学习第十一天 -- 函数式编程
在介绍函数式编程之前,先介绍几个概念性的东西. 什么是函数式编程? 函数式编程的特点: 1.把计算视为函数而非指令; 2.纯函数式编程:不需要变量,没有副作用,测试简单; 3.支持高阶函数,代码简洁. ...
- ida idc函数列表全集
下面是函数描述信息中的约定: 'ea' 线性地址 'success' 0表示函数失败:反之为1 'void'表示函数返回的是没有意义的值(总是0) AddBptEx AddBpt AddCodeXre ...