继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

【九月打卡】第21天 go语言 TS环境与语法

洋溢1310659
关注TA
已关注
手记 26
粉丝 2
获赞 4

课程名称GO开发工程师

课程章节:第一章:TS环境搭建预配置;第二章:TS与JS;第三章:TS语法基础

课程讲师ccmouse

课程内容

一. TS环境的搭建与配置

# colorcar

## 如何编译以及运行小程序
1. `cd wx`
2. `npm install`
3. 打开小程序开发工具,点击编译(npm run tsc),编译成js文件

二. TS 特有类型

literal type union types enum用法
示例:

// literal type => 类似枚举
let answer: 'yes' | 'no' | 'maybe' = 'maybe'
let literalHttpStatus: 200 | 404 | 500 = 200
function f(s: 200 | 404 | 500) {
    let status: number = s
    console.log(status)
}
f(200)

// union types =>  可以有多种类型
let unionHttpStatus: 200 | 404 | 500 | '200' | '404' | '500' = '200'

function unionF(s: 200 | 404 | 500 | '200' | '404' | '500') {
    // let status: number = s 
    // error: 
    //  Type 'string | number' is not assignable to type 'number'.
    //  Type 'string' is not assignable to type 'number'

    let status: number | string = s // 这里需对应参数的几种类型
    console.log(status)
}
unionF(200)
unionF('200')

// undefined
let status1: undefined =  undefined // 只能是undefined
// status1 = 1 //error: Type '1' is not assignable to type 'undefined'

// 定义枚举值
enum HttpStatus {
    OK = 200,
    NOT_FOUNT = 404,
    INTERNAL_SERVER_ERROR = 500,
}

function processHttpStatus(s: HttpStatus) {
    if (s === HttpStatus.OK) {
        console.log('good response')
    } else {
        console.log('bad response')
    }
       console.log(HttpStatus[s]) // 打印code对应的字符 OK、NOT_FOUNT、INTERNAL_SERVER_ERROR
}
processHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR)

enum TimingFunc {
    LINEAR = 'linear',
    EASEIN = 'easeIn',
    EASEOUT = 'easeOut',
    EASEINOUT = 'easeInOut'
}

function annimation(t: 
| 'linear' 
| 'easeIn'
| 'easeOut' 
| 'easeInOut') {
    // console.log(t)
    // console.log(TimingFunc[t]) // 值为字符串的无法打印
}
annimation(TimingFunc.EASEIN)

课程收获

ts:约束类型,提前发现错误。

图片描述
图片描述

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP