亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

【九月打卡】第21天 go語言 TS環境與語法

標簽:
Go

课程名称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:约束类型,提前发现错误。

图片描述
图片描述

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消