Typescript 查缺补漏
Types
Casting:
let input = xxx as HTMLInputElement;
let input = <HTMLElement>xxxx;
Object Shapes:
Typescript only cares about the shape of an object.
Interfaces:
- only describe structure, no implementation
- don't compile to any js code
- DRY, easy refactoring
- open, can be extended later on. (extends)
any:
never:
Nothing can be assigned to never.
function return type void.
Classes
still protofypal inheritance, but better syntax.
static method:
static createPerson() {
}
instance / public fields:
class Person {
static population = 122; // public (static) field
country = 'China'; // instance field constructor(name) {
}
}
inheritance:
class Person : Human {
constructor() {
super();
}
}
super.xxx(); // function invoke.
Species:
static get [Symbol.species]() {
return Array;
}
Mixins:
Enums:
enum Gender {
Male,
Female
}
Arrays:
Tuples:
Fixed length.
let t2: [string, number];
t2 = ['angular', 1];
Type Aliases:
interface sometimes is not good enough.
type keyword to define a type alias:
type Color = [number, number, number];
let red: Color = [255, 0, 0];
can be exported.
Object Literals
Enhanced:
let person = {
__proto__ : String.prototype,
name: 'Dave',
company,
[`${company}Title`]: 'CTO',
toString() {
return `${super.toString()}xxx`;
}
};
Destructured Assignment:
Rest, Spread Properties:
...
rest: and the rest goes here
spread: and all the properties on this object.
Getters, Setters:
Function - Types:
Functions have a type just like any other value.
interface can also describe functions:
interface ClickListener {
(this: Window, e: MouseEvent): void
} const myListender: ClickListener = (e) => {
console.log(e);
};
this, calling context must be window.
Function - Parameters:
named, optional, default value, rest parameters(...).
Generics
let persons = Array<[String, String]>(20);
can specify constraints on generic types:
function calc<T extends number>(x: T, y: T) { }
can also be use with interface:
interface IFileReader<T extends File> {
read(file: T): Blod
}
Access Modifier Keywords:
public, protected, private
Function overloading:
Allows us to have more than one function "head", but a single implementation.
function add(x: number, y: number): number; // this pattern ok
function add(x: string, y: string, radix: number): number; // this pattern ok function add(x: number | string, y: number | string, radix: number = 10): number { // must match 2 patterns above.
return parseInt(`${x}`, radix) + parseInt(`${y}`, radix);
}
add(1, '4'); // not ok
Iterators & Generators
Iterators: keeping track of current position, next()
Iterables:
- support for .. of loop.
- Requires implementation of Symbol.iteractor method
- Array, Map already support it.
Generators:
- yield
- function*() syntax
- returns an iterator
- State of closure is preserved between .next() calls.
- Execution Can be Paused (yield).
it.next() goes in the loop, and pause after yield until next it.next() call.
Iterators:
The ability to pass values in while iterating.
console.log(it.next(134).value);
yield* keyword.
yield* [1, 2, 3];
Typescript 查缺补漏的更多相关文章
- Android查缺补漏--Activity生命周期和启动模式
一.生命周期 onCreate():启动Activity时,首次创建Activity时回调. onRestart():再次启动Activity时回调. onStart():首次启动Activity时在 ...
- Android查缺补漏--BroadcastReceiver的类型与使用
Broadcast 是一种被用于应用内和应用之间传递信息的机制.一个广播可以对应多个接受者.一个完整的广播机制,需要具有以下三个要素: 发送广播的Broadcast 接受广播的BroadcastRec ...
- Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?
在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...
- Android查缺补漏--ContentProvider的使用
ContentProvider (内容提供者)是一种共享型组件,可以为系统内应用于与应用之间提供访问接口. ContentProvide要想正常工作需要三个关键点: ContentProvider:对 ...
- Android查缺补漏--Service和IntentService
Service的运行不依赖界面,即使程序被切换到后台,Service仍然能够保持正常运行.当某个应用程序进程被杀掉时,所有依赖于该进程的Service也会停止运行. Service 分为启动状态和绑定 ...
- Android查缺补漏(View篇)--自定义 View 的基本流程
View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...
- Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解
上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas和Paint,从细节上更进一步的讲解自定 ...
- Android查缺补漏(View篇)--事件分发机制
事件分发机制是Android中非常重要的一个知识点,同时也是难点,相信到目前为止很多Android开发者对事件分发机制并没有一个非常系统的认识,当然也包括博主个人在内.可能在平时的开发工作中我们并没有 ...
- Android查缺补漏(View篇)--事件分发机制源码分析
在上一篇博文中分析了事件分发的流程及规则,本篇会从源码的角度更进一步理解事件分发机制的原理,如果对事件分发规则还不太清楚的童鞋,建议先看一下上一篇博文 <Android查缺补漏(View篇)-- ...
随机推荐
- ubuntu更换pip install,apt-get,conda install 成国内源
解决ubuntu的pip和apt-get太慢的问题 ubuntu国外龟速的源实在难受,还是自己动手更改一下各种pip 源和apt-get 的源吧,换了之后速度令人舒适! 更换pip源成清华源 临时使用 ...
- 575. Distribute Candies
https://leetcode.com/problems/distribute-candies/description/ 题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一 ...
- *CTF 2019 quicksort、babyshell、upxofcpp
这次参加比赛总共出了三道,有两道队友都先交了,还是tcl,heap_master卡了差不多一天没解决....还是记录一下出的题目吧 quicksort 题目大体流程就是输入要输入的数字数量,然后输入数 ...
- 032 Java再次总结
1.大纲 多线程怎么用的,线程池,事务,传播机制与隔离级别,反射,泛型,数据库引擎的区别,数据库merge,窗口函数,fastJson,JVM调优,GC钩子,Linux的awk,shell,HashM ...
- HCNA(华为)_DHCP篇
在大型的企业网络中,会有大量的主机或设备需要获取IP地址等网络参数.如果采用手工配置,工作量大 且不好管理,如果有用户擅自修改网络参数,还有可能会造成IP地址冲突等问题.使用动态主机配置协DHCP 来 ...
- [POJ1193][NOI1999]内存分配(链表+模拟)
题意 时 刻 T 内存占用情况 进程事件 0 1 2 3 4 5 6 7 8 9 进程A申请空间(M=3, P=10)<成功> 1 A 2 A B 进程B申请空间(M=4, P=3)< ...
- 使用PIA查找组件的PeopleSoft导航
导航到企业组件>查找对象导航. 使用组件名称 使用页面名称 使用辅助页面名称 使用内容参考名称 只需输入对象名称,然后单击“搜索”即可.在这个例子中.我们知道组件名称即'PRCSDEFN',我们 ...
- 微信小程序开发---视图层(View)
WXML WXML能力: 数据绑定 列表渲染 条件渲染 模板 事件 数据绑定 数据绑定使用 Mustache 语法(双大括号)将变量包起来,可作用于内容,组件属性(需要在双引号之内),控制属性(需要在 ...
- 二分- Count on Canton
题目: 代码: 是一个蛇形数列,把题目上的那组数倒过来看成一个正三角形. 第一行有1个数,1-2行有三个数,1-4行有6个数,1-4行有10个数,1-5行有15个数..... 现在要求第n个数是多少, ...
- http://www.layui.com/doc/modules/laydate.html实时通信\日期、==插件
8520**ali chengyouli http://www.layui.com/doc/modules/laydate.html实时通信\日期.==插件