[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则是一种半面向对象 ...
随机推荐
- windows下php+apache+mysql环境搭建
在Windows 7下进行PHP环境搭建,首先需要下载PHP代码包和Apache与Mysql的安装软件包. PHP版本:php-5.3.2-Win32-VC6-x86,VC9是专门为IIS定制的,VC ...
- (第三章)Java内存模型(上)
一.java内存模型的基础 1.1 并发编程模型的两个关键问题 在并发编程中,需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体).通信是指线程之间以何种机制来 ...
- C#逻辑运算符详解
代码如下: namespace ConsoleApplication1 { class @class { static void Main_1(string[] args) //输出用户输入的内容 { ...
- Json格式的http请求
服务端PHP代码可以从这里下载:https://github.com/lornajane/PHP-Web-Services 1.使用volley实现: request要用JsonObjectReque ...
- displaytag 添加超链接
方法1:在<display:column>中使用属性autolink="true",不过这条属性只对两种情况起作用:网址和邮箱,如果只是普通字符串,那么还是普通字符串, ...
- 使用Alcatraz为Xcode安装XActivatePowerMode插件, 从此敲代码逼格大大滴~
Alcatraz 是一款 Xcode的插件管理工具,可以用来管理XCode的 插件.模版以及颜色配置的工具. 关于Alcatraz的安装,这里有一篇不错的博文,请参考安装:http://www.cnb ...
- 【转】char *str 和 char str[]的区别
char str[] = "abcd";定义了一个局部字符数组,返回它的地址肯定是一个已经释放了的空间的地址. 此函数返回的是内部一个局部字符数组str的地址,且函数调用完毕后 此 ...
- (原)ubuntu14.04中安装gcc4.9和g++4.9
http://stackoverflow.com/questions/28683747/installing-gcc4-9-on-ubuntu-14-04-lts http://askubuntu.c ...
- PHP打开PDO_MySQL扩展的配置方法
PHP中的PDO其实是一个很好用的扩展,在一些PHPCMS系统中,开发者大多都有用到,那么如何开启PDO和PDO_MySQL扩展呢?方法同样很简单: 打开php.ini配置文件,找到extension ...
- pyqt5猜数游戏
电脑随机生成4个互不相等的数,你猜. 1:你猜的数和正确答案相比,位置正确的个数 2:你猜的数虽然在正确答案中,但位置不对,它的个数. 代码如下: #!/usr/bin/env python # -* ...