0%
整一波 ts
2021-2-26 0:54:34
安装
shell
npm install -g typescript
运行
shell
tsc index.ts
基础类型
布尔值、数字
typescript
let isDone: boolean = false
let dec: number = 6
let hex: number = 0xf00d
let bin: number = 0b1010
let oct: number = 0o744
console.log(isDone, dec, hex, bin, oct)
字符串 模板字符串
typescript
let onename: string = "bob"
onename = "smith"
let name2: string = `Gene`
let age: number = 37
let sentense: string = `my name is ${name2}.
I'll be ${age + 1} years old next month`
console.log(onename, name2, sentense)
数组
typescript
let list: number[] = [1, 2, 3]
let list2: Array<number> = [1, 2, 3]
console.log(list, list2)
元组
typescript
let x: [string, number]
x = ['hello', 10]
枚举
typescript
enum Color {Red, Green, Blue}
let c: Color = Color.Green
let colorName: string = Color[2]
console.log(c, colorName)
any
typescript
let notSure: any = 4
// console.log(notSure.ifItExist(), notSure.toFixed())
let prettySure: Object = 4
// console.log(prettySure.toFixed())
let list1: any[] = [1, true, "free"]
console.log(list1)
list1[1] = 100
console.log(list1)
void
function warnUser(): void {
console.log("warning!")
}
let unusable: void = undefined
console.log(warnUser(), unusable)
undefined 和 null
没啥用 undefined和null是任何类型的子类型
typescript
let u: undefined = undefined
let n: null = null
console.log(u, n)
never
never 表示永不存在的值的类型
never 也是任何类型的子类型
typescript
function error(message: string): never {
throw new Error(message)
}
console.log(error("error!"))
object
表示非原始类型,除了number string boolean symbol null undefined之外的类型
typescript
declare function create(o: object | null): void;
create({ prop: 0})
create(null)
断言
typescript
let someValue: any = "string"
let strLen: number = (<string>someValue).length
console.log(someValue, strLen)
let someValue2: any = "string"
let strLen2: number = (someValue2 as string).length
console.log(someValue2, strLen2)
变量声明
let是块作用域
多用let少用var
同一个作用域里一个变量只能let一次
const作用域跟let一样,声明完了不能再改
解构
typescript
let input: [number, number] = [1, 2]
let [first, second] = input
console.log(first, second)
function f([first, second]: [number, number]) {
console.log(first, second)
}
f(input)
typescript
let [one, ...rest] = [1, 2, 3, 4]
console.log(one, rest)
// 1 [2, 3, 4]
太乱了不研究了用上了再说。
接口
typescript
interface SquareConfig {
color?: string,
width?: number
}
function createSquare(
config: SquareConfig
): {color: string; area: number} {
let newSquare = {color: "white", area: 100}
if (config.color) {
newSquare.color = config.color
}
if (config.width) {
newSquare.area = config.width * config.width
}
return newSquare
}
let mysquire = createSquare({color: "black"})
console.log(mysquire)
函数类型的接口
typescript
interface SearchFunc {
(
source: string,
subString: string
): boolean
}
let mySearch: SearchFunc = function(
source: string,
subStirng: string
) {
let result = source.search(subStirng)
return result > -1
}
console.log(mySearch("abc", 'a'))
可索引的类型
typescript
interface StringArray {
[index: number]: string
}
let myarray: StringArray = ["bob", "fred"]
let myStr: string = myarray[0]
console.log(myarray, myStr)