• 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];
    

     

    函数类型

    type in line

    let add=function(x:number,y?:number):string{
        return 'asdfasdf'
    }
    
    
    let add1=(x:string,y?:string):number=>{
        return 21
    }
    

      

    interface

    interface Foo {
        (a: string | number, b: string | number): string | number
    }
    let foo: Foo = (a, b) => {
        return a + b
    }
    

      

    type

    type Foo = (a: string, b: string) => string
    let foo: Foo = (a, b) => {
        return a + b
    }
    // or
    type Foo = {
        (a: strin, b: string): string
    }
    let foo: Foo = (a, b) => {
        return a + b
    }
    

      

    函数重载

    interface Foo {
      (a: string, b: string): string
      (a: number, b: number): number
    }
    
    let foo1: Foo = (a, b) => {
      return a + b
    }
    

      

    function foo(a: string | number, b: string | number): string | number {
        return a + b
    }
    
    let a = foo('a', 'b') // 'ab': string
    let b = foo(0, 1)     // 1: number
    let c = foo('a', 1)   // error: No overload matches this call.
    

      

    type interface 区别

    •   type 可以声明基本类型别名,联合类型,元组等类型
    • interface 能够声明自动合并

     

  • 相关阅读:
    Cocos2d-x之Vector<T>
    Cocos2d-x之Array
    Cocos2d-x之Value
    Cocos2d-x之String
    Cocos2d-x中使用的数据容器类
    Cocos2d-x之Action
    Cocos2d-x之定时器
    Cocos2d-x之MessageBox
    Cocos2d-x之Log输出机制
    Cocos2d-x之事件处理机制
  • 原文地址:https://www.cnblogs.com/breakdown/p/13827480.html
Copyright © 2020-2023  润新知