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 查缺补漏的更多相关文章

  1. Android查缺补漏--Activity生命周期和启动模式

    一.生命周期 onCreate():启动Activity时,首次创建Activity时回调. onRestart():再次启动Activity时回调. onStart():首次启动Activity时在 ...

  2. Android查缺补漏--BroadcastReceiver的类型与使用

    Broadcast 是一种被用于应用内和应用之间传递信息的机制.一个广播可以对应多个接受者.一个完整的广播机制,需要具有以下三个要素: 发送广播的Broadcast 接受广播的BroadcastRec ...

  3. Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?

    在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...

  4. Android查缺补漏--ContentProvider的使用

    ContentProvider (内容提供者)是一种共享型组件,可以为系统内应用于与应用之间提供访问接口. ContentProvide要想正常工作需要三个关键点: ContentProvider:对 ...

  5. Android查缺补漏--Service和IntentService

    Service的运行不依赖界面,即使程序被切换到后台,Service仍然能够保持正常运行.当某个应用程序进程被杀掉时,所有依赖于该进程的Service也会停止运行. Service 分为启动状态和绑定 ...

  6. Android查缺补漏(View篇)--自定义 View 的基本流程

    View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...

  7. Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解

    上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas和Paint,从细节上更进一步的讲解自定 ...

  8. Android查缺补漏(View篇)--事件分发机制

    事件分发机制是Android中非常重要的一个知识点,同时也是难点,相信到目前为止很多Android开发者对事件分发机制并没有一个非常系统的认识,当然也包括博主个人在内.可能在平时的开发工作中我们并没有 ...

  9. Android查缺补漏(View篇)--事件分发机制源码分析

    在上一篇博文中分析了事件分发的流程及规则,本篇会从源码的角度更进一步理解事件分发机制的原理,如果对事件分发规则还不太清楚的童鞋,建议先看一下上一篇博文 <Android查缺补漏(View篇)-- ...

随机推荐

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'r ...

  2. POSIX Timer

    SYNOPSIS #include <signal.h> /* only timer_create need this header */ #include <time.h> ...

  3. 记一次线上Zabbix对Redis监控实录

    前言:Redis作为缓存服务器我想大家都比较的熟悉,那么,如果想要更好的维护和监控,那么我们会对其redis服务器统一监控起来,如何监控呢?如果在生产环境一台服务器部署多个redis,这样就会出现多个 ...

  4. php换行和<br />互转

    使用场景:在后台处理textarea换行的时候出现了问题, textarea里面的换行就是/n, 在textarea里面是有换行效果的,但是输出到其它地方没有效果,这时候就要用到PHP的神奇的nl2b ...

  5. Linux-硬件

    1.服务器 计算节点服务器-用于后台逻辑运算,所以cpu,磁盘读写性能要求较高 web服务器-用于用户请求访问一些页面,如果高并发,磁盘读写性能要好,可以使用raid0或raid1或raid5技术(r ...

  6. WinForm 中 comboBox控件之数据绑定

    一.IList 现在我们直接创建一个List集合,然后绑定 1 IList<string> list = new List<string>(); 2 list.Add(&quo ...

  7. hdu1814 Peaceful Commission

    hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...

  8. 2018-4-5-cadence skill

    skill 是 Cadence 提供的二次开发语言,可以做很多有用的二次开发. 开发参考手册:<algroskill><sklangref><sklanguser> ...

  9. (lua) 基于cocos 的插入光效

    具体的表现是:移动滚动容器到头部, 新增icon淡入并掉入格子,显示格子中的图标,icon放大并淡出 function UISkyResource:playActivedEffect(id) then ...

  10. css实现中文换行,英文换行,超出省略

    英文换行时,是以单词换行,在对应的标签添加对应的属性即可 1 word-break:break-all;只对英文起作用,以字母作为换行依据 2 word-wrap:break-word; 只对英文起作 ...