zoukankan      html  css  js  c++  java
  • typescript handbook 学习笔记1

    概述

    这是我学习typescript的笔记。写这个笔记的原因主要有2个,一个是熟悉相关的写法;另一个是理清其中一些晦涩的东西。供以后开发时参考,相信对其他人也有用。

    学习typescript建议直接看中文文档英文文档。我是看的英文文档

    介绍

    我不过多的介绍typescript,因为网上资料一大堆。我只记录下我的个人见解

    javascript是一个很灵活的语言,在维护时就会遇到很多坑,所以我们选择用typecript。

    typecript的如下几点非常吸引我,并且非常激动人心

    1. 代码编写者的最低权限原则,又叫Principle of least privilege。我们编写代码的时候,应该赋给代码编写者最低的权限,而不是过多的权限,否则代码编写者就会使用这过多的权限给别人挖一些坑。
    2. 参数注释和检查。以前我们在写一个函数的时候需要在函数上面写一些关于这个函数的注释:参数是什么,返回什么等。而typescript直接把他们集成到代码中去了,并且能够为这些提供检查。
    3. 函数式编程思想。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;
    
  • 相关阅读:
    UVA1349 Optimal Bus Route Design 最优巴士路线设计
    POJ3565 Ants 蚂蚁(NEERC 2008)
    UVA1663 Purifying Machine 净化器
    UVa11996 Jewel Magic 魔法珠宝
    NEERC2003 Jurassic Remains 侏罗纪
    UVA11895 Honorary Tickets
    gdb调试coredump(使用篇)
    使用 MegaCLI 检测磁盘状态并更换磁盘
    员工直接坦诚直来直去 真性情
    山东浪潮超越3B4000申泰RM5120-L
  • 原文地址:https://www.cnblogs.com/yangzhou33/p/8642131.html
Copyright © 2011-2022 走看看