typescript handbook 学习笔记1
概述
这是我学习typescript的笔记。写这个笔记的原因主要有2个,一个是熟悉相关的写法;另一个是理清其中一些晦涩的东西。供以后开发时参考,相信对其他人也有用。
学习typescript建议直接看中文文档或英文文档。我是看的英文文档。
介绍
我不过多的介绍typescript,因为网上资料一大堆。我只记录下我的个人见解。
javascript是一个很灵活的语言,在维护时就会遇到很多坑,所以我们选择用typecript。
typecript的如下几点非常吸引我,并且非常激动人心。
- 代码编写者的最低权限原则,又叫Principle of least privilege。我们编写代码的时候,应该赋给代码编写者最低的权限,而不是过多的权限,否则代码编写者就会使用这过多的权限给别人挖一些坑。
- 参数注释和检查。以前我们在写一个函数的时候需要在函数上面写一些关于这个函数的注释:参数是什么,返回什么等。而typescript直接把他们集成到代码中去了,并且能够为这些提供检查。
- 函数式编程思想。typescript用到了少许函数式编程思想。比如下面这个例子。
function foo() {
// okay to capture 'a'
return a;
}
// illegal call 'foo' before 'a' is declared
// runtimes should throw an error here
foo();
let a;
基本类型
任何语言都有基本类型,typescript也是一种语言,也不例外。在声明这些基本类型的时候需要指定它们的类型。
Boolean布尔值
let myBoolean: boolean = false;
Number数字
let myNumber: number = 22;
String字符串
let myString: string = 'haha';
Array数组
Array有2种写法,我个人推荐下面这种。
//推荐
let myList: number[] = [1, 2, 3];
//不推荐,仅作熟悉
let myList: Array<number> = [1, 2, 3];
Tuple元组
let tuple01: [string, number];
//不止2个
let tuple02: [string, boolean, number];
//继续赋值会使用union type: string | number
tuple01 = ['hello', 10];
tuple01[5] = 'haha';
Enum枚举
//注意没有分号,并且后面要加: Color声明
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
//用数字引用元组内容,注意是string?
enum Color {Red = 1, Green = 3, Blue = 7}
let colrName: string = Color[3];
Any
//Any无比强大
let notSure: any = 4;
notSure = 'haha';
notSure = false;
notSure.toFixed();
//数组
let list: any[] = [1, true, 'haha'];
Void
//Void一般用于函数的返回值,表示没有返回值
function warnUser: void {
alert('This is my warning message!');
}
Null, Underfied和Never
null和underfied是其它任何类型的子类型,所以可以把它们赋值给其它类型。
never一般在抛出异常和从不返回的函数中用到。(注意:never是从不返回,never returns;而void是没有返回值)
//抛出异常
function error(message: string): never {
throw new Error(message);
}
//无限循环,从不返回
function infiniteLoop(): never {
while (true) {
}
}
Type assertions类型断言
类型断言有2中形式,我们推荐下面这种。类型断言表示这个类型我能确定是这个,不用类型检查了,一般用于跳过typescript的类型检查。
//推荐
let someThing: any = 'this is haha';
let strLength: number = (someThing as string).length;
//仅作熟悉,不推荐
let someThing: any = 'this is haha';
let strLength: number = (<string>someThing).length;
typescript handbook 学习笔记1的更多相关文章
- typescript handbook 学习笔记4
概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...
- typescript handbook 学习笔记3
概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...
- typescript handbook 学习笔记2
概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...
- typescript类(学习笔记非干货)
我们声明一个 Greeter类.这个类有3个成员:一个叫做greeting的属性,一个构造函数和一个greet方法. We declare a Greeter class. This class ha ...
- typescript接口(学习笔记非干货)
typescript的核心原则之一就是对所具有的shape类型检查结构性子类型化 One of the core principles of typescript is to check struct ...
- 【TypeScript】学习笔记 把一些需要记的记录一下
安装typescript: npm install -g typescript 启动typesctipt自动编译: tsc 文件名.ts --watch 函数参数默认值: 1.有默认值参数的,声明在最 ...
- typescript泛型(学习笔记非干货)
软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型, 这在创建大型系统时为你提供了十分灵活的功能. In softwa ...
- TypeScript语言学习笔记(2)
接口 // 在参数类型中定义约束 function printLabel(labelledObj: { label: string }) { console.log(labelledObj.label ...
- TypeScript语言学习笔记(1)
基本类型 // 布尔型(Boolean) let isDone: boolean = false; // 数值型(Number) let decimal: number = 6; let hex: n ...
随机推荐
- Spring.Net配置
<?xml version="1.0" encoding="utf-8"?> <configuration> <!--一定 ...
- GIT TEAMWORK
Learn GIT TEAMWORK generalizations Congratulations, you now know enough to start collaborating on Gi ...
- Spring注解方式配置Redis
@Configuration public class RedisConfiguraion { @Bean public JedisConnectionFactory redisConnectionF ...
- QUARTZ系列之一-基础概念(Scheduler/Job/JobDetail/Trigger)
摘抄自quartz官方文档: The key interfaces of the Quartz API are: Scheduler - the main API for interacting wi ...
- 8-13、Python 散列复习
1.{} 输入是花括号 myCat = { 'size':'fat', 'color':'gray', 'disposition':'loud'} 键:值 myCat['size'] = fat ...
- AltiumDesigner 查找相似对象
同类器件的集体选中.集体选中的方法是:先选中一个标识符,右击在选项表中选择Find Similar Objects,然后就会出现一个对话框,在这个对话框中,有一些any项,根据自己的需要把一些any改 ...
- python note 05 字典及其操作
1. '''#数据类型划分:可变数据类型,不可变数据类型不可变数据类型:元组,bool int str 可哈希可变数据类型:list,dict set 不可哈希dict key 必须是不可变数据类型, ...
- 348. Design Tic-Tac-Toe设计井字游戏
[抄题]: Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume t ...
- spring 自定义标签的实现
在我们进行Spring 框架开发中,估计用到最多的就是bean 标签吧,其实在Spring中像<mvc/><context/>这类标签以及在dubbo配置的标签都是属于自定义的 ...
- 回溯算法_ BackTracking
目前还存在的疑问: 1. 所谓的该分支满足条件之后就回退到上一层节点,可是加谁呢? x[i+1] ?? 加到 N, 不满足target sum条件就返回上一级(同时改变上一级数为 i+1...纵向 ...