[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则是一种半面向对象 ...
随机推荐
- mevan引入容联云通讯jar
首先从官网下载jar 然后拷贝到lib目录下 最后在pom.xml中这样写 <dependency> <groupId>cn.com</groupId> <a ...
- Java基础知识强化40:StringBuffer类之StringBuffer的替换功能
1. StringBuffer的替换功能: public StringBuffer replace(int start, int end, String str): 2. 案例演示: p ...
- LDAP索引及缓存优化
一.设置索引 索引将查找信息和 Directory Server 条目关联起来. Directory Server支持以下几种索引: 1出现索引 (pres) - 列出了具有特定属性的条目,与属性的值 ...
- nginx反向代理nginx,RealServer日志打印真实ip
title: nginx反向代理nginx,RealServer日志打印真实ip date: 2016-05-11 19:15:37 tags: --- nginx反向代理nginx,RealServ ...
- Facebook登录 AndroidStudio
简介 主页:https://www.facebook.com/ Android开发者支持:https://developers.facebook.com/docs/android/ 应用管理:htt ...
- (二)《Java编程思想》——t h i s 关键字
this 关键字(注意只能在方法内部使用)可为已调用了其方法的那个对象生成相应的句柄.可象对待其他任何对象句柄一样对待这个句柄. package chapter4; //: Leaf.java // ...
- CSS投影实现方式
备用素材: 1.png shadow.png 第一种方式: 利用负边距实现 最终效果图: <!DOCTYPE html> <html lang="en"&g ...
- jQuery选择器 之详述
jQuery选择器 一. 单词小计 Pervious 上一页sibling 同级first 第一last 最后not 不 Even 偶数 odd 奇数 header 页眉 一.jQ ...
- (四)JS学习笔记 - 模式 - 观察者模式
理解观察者模式 简单的讲,一个对象作为特定任务或是另一对象的活动的观察者,并且在这个任务或活动发生时,利用事件的形式通知观察者. 观察者的使用场合 当一个对象的改变需要同时改变其它对象,并且它不知道具 ...
- web前端安全相关
burpsuite Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程.所有的工具都共享一个能处理并显示HTTP 消 ...