TypeScript 素描

/*
交叉类型,在TypeScrpt中是很特有的.所以值得认真学习
交叉类型是将多个类型合并为一个类型,这让我们可以把现有的多种类型叠加到一起成为一种
类型
交叉类型同时拥有 Person 和 Employee的成员
*/
let 交叉类型 = <Person & Employee>{};

/*
 联合类型,当方法的参数可以是string又可以是number的时候怎么办? 我们想要给方法的调
用者一个明确的提示 所以使用 any ?? 这自然是不对的,因为参数了可以传布尔,使用联合类
型完美解决此问题  Type | Type
*/
function 联合类型(arg: string | number): void | string {
    if (typeof arg == "string") {
        return arg;
    }
}


/*
类型保护  因为有可能我们不准确变量的类型,但是知道一个范围
我们可以使用 类型断言 <type> typeof interfaceof
*/

/*
类型别名 给类型起一个别名  type Container<T> = { value: T };
*/

/*
字符串字面量类型  允许我们为 string变量提供必须的固定值 ,也就是字符串的值必须是我
们内定的
*/
type Easing = "A" | "B" | "C";
function fun13(str: Easing) { };
fun13("A"); //这里只可以传 A B C


/*
有趣的链式编程来自 多态的this
*/
class BasicCalculator {
    public Add(): this {
        return this;
    };
    public multiply(): this {
        return this;
    };
    public Abs(): this {
        return this;
    };
}
let bc = new BasicCalculator();
bc.Add().Abs().multiply();

/*
Symbol 自Es6起 symbol成为了一个新的原生类型,就像stirng、number一样
symbol类型的值是通过symbol构造函数创建的,且symbol是不可改变且唯一的
-- 不知道为什么在vs2015里无法使用
*/

/*
迭代器
for in  拿到的是下标
for of  拿到的是值 
*/

for (let i in array) { };
for (let i of array) { };
原文地址:https://www.cnblogs.com/LiangSW/p/6262122.html