typescript

基础类型
    布尔
        let isDone: boolean = false;
    数字
        let decLiteral: number = 6;
    字符串
        let name: string = "bob";
    数组
        let list: number[] = [1, 2, 3];
        let list: Array<number> = [1, 2, 3];
     let ro: ReadonlyArray<number> = [1, 2, 3]; 元组 let x: [
string, number]; x = ['hello', 10]; x[3] = 'world'; // string | number 枚举 enum Color {Red, Green, Blue} // 默认0开始 let c: Color = Color.Green; Any let notSure: any = 4; Void function warnUser(): void {} Null和Undefined 非--strictNullChecks模式下是任何类型的子类型 Never 永不为真,存在无法到达的终点 Object 类型断言 let someValue: any = "this is a string"; let strLength: number = (<string>someValue).length let strLength: number = (someValue as string).length; // jsx中必须

 

变量声明
    var
        函数作用域
        变量提升
    let const
        块级作用域
    解构
        function f([first, second]: [number, number]) {}
        let {a, b}: {a: string, b: number} = o;
    默认值
        function f(object: { a: string, b?: number }) {} // ?可选
        ...c自身的可枚举属性

 

接口
    interface LabelledValue {
        label: string;
        name?: string;
        readonly x: number;
    }
    额外检查
        不允许出现interface未出现的属性
        用as重新转换解决
        索引签名
            interface SquareConfig {
                color?: string;
                width?: number;
                [propName: string]: any;
            }
        赋值变量
            let squareOptions = { colour: "red", width: 100 };
            let mySquare = createSquare(squareOptions);
函数类型接口
    interface SearchFunc {
      (source: string, subString: string): boolean;
    }
    let mySearch: SearchFunc;
    mySearch = function(src: string, sub: string): boolean {}

 

posted @ 2019-03-05 14:36  王利群  阅读(158)  评论(0编辑  收藏  举报