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