为了账号安全,请及时绑定邮箱和手机立即绑定

【九月打卡】第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下载
官方微信

举报

0/150
提交
取消