TS数据类型及语法

// 编译命令
// tsc .\index.ts
// 执行文件
// node index.js 

// cnpm install -g ts-node
// 执行ts文件
// ts-node index.ts

// 热编译 执行命令即可
// cnpm install -g nodemon
// nodemon --exec ts-node .\index.ts

var fn = () => 'geohot'

// 类型声明
let anub: number = 1
let str: string = 'string'
let bloo: boolean = true

// 数组
let myArr: number[] = [1,2.3,3]
let myArrsa: any[] = [1,2.3,'111']
let aa: Array<string> = ['111','222']
let bb: Array<boolean> = [false, true]

// 元组
let myArrs: [number, string] = [1,'sa']

// 函数
function add(a: number, b: number) { 
    return a + b
}
let sum = add(10, 20)
console.log(sum)

const adds = (a: number, b: number) => { 
    return a + b
}
let sums = adds(10, 20)
console.log(sums)

// 函数返回参数类型
const addss = (a: number, b: number):number => { 
    return a + b
}
let sumss = addss(10, 20)
console.log(sums)
// 函数默认值
const addsss = (a: number, b: number = 10):void => { 
   a + b
}
let sumsss = addsss(10)
console.log(sumsss)
// 函数可选参数
const addssss = (a: number, b?: number):void => { 
    if (b) {
        console.log(a + b)
    } else { 
        a
    }
 }
 let sumssss = addssss(10)
 console.log(sumsss)

//  表示c可以等任何类型
let c: any
let myArrsas: any[] = [1,2.3,'111']

// 表示传的值可以是number或者string
let log = (values: number | string) => { 

}
// class
class Person { 
    name: string
    constructor() { 
        this.name = 'Geohot.J'
    }
    increase(val: number) { 

    }
}

// 枚举类型
enum DaysOfTheWeek {
    SUM, MON
}
let day: DaysOfTheWeek
day = DaysOfTheWeek.MON
console.log(day)


// 接口类型
interface Named { 
    name: string
    inter()
    inters(parmas: string): void
}
const Persons = {
    name: 'Geohot.J.lin',
    inter: () => { 
        console.log("没有参数的方法")
    },
    inters: (name: string) => { 
        console.log(name)
    }
}
const sayName = (o: Named) => {
    console.log(o.name)
    o.inter()
    o.inters(o.name)
}

sayName(Persons)

// 类型别名
type Name = string
let myName: Name = '我是类型别名'

// 匿名函数
interface PrintCallback {
    // 匿名函数
    (success: boolean): void
}
let PrintCallback: PrintCallback
PrintCallback = (suc: boolean):void => {

}

// 类型断言

// 泛型

  

posted @ 2021-01-12 10:57  爱跑步的乌龟  阅读(425)  评论(0编辑  收藏  举报