數(shù)據(jù)類型
基本數(shù)據(jù)類型(undefined和null可以賦值給任何基礎(chǔ)類型)
let str: string = 'xiaoming' // 字符串
let num: number = 123 // 數(shù)字
let bool: boolean = false // 布爾值
let ud: undefined = undefined // undefined
let nll: null = null // null
引用數(shù)據(jù)類型
數(shù)組和元組
let arr: bumber[] = [1, 2, 3] // 只能存儲(chǔ)數(shù)字類型的數(shù)據(jù)
let arr: [string, number] = ['xiaoming', 123] // 元組庵楷,對(duì)存儲(chǔ)的數(shù)據(jù)進(jìn)行限制
inrerface
: 接口
對(duì)對(duì)象形狀(shape)進(jìn)行描述
對(duì)類進(jìn)行抽象
Duck Typing(鴨子類型)
interface IPerson {
name: string;
age: number;
}
let obj: IPerson = {
readonly id: number; // 只讀屬性,只在第一次執(zhí)行時(shí)賦值,后面再賦值報(bào)錯(cuò)
name: string;
age?: 18; // 加入?表示可選屬性
}
函數(shù)申明式寫法
如果有默認(rèn)值,他就會(huì)自動(dòng)變成可選參數(shù)
如果要表示可選參數(shù)使用方式:z?: number
,注意:可選參數(shù)只能放在參數(shù)的最后一個(gè)
function add(x: number, y: number, z: number = 10) : number {
if(typeof z === 'number') {
return x + y + z
} else {
return x + y
}
}
函數(shù)表達(dá)式的寫法
add
是一個(gè)函數(shù)類型脉顿,ts會(huì)在沒有明確指定類型時(shí)進(jìn)行類型推論,不能再將它賦值給其他類型
const add = function(x: number, y: number, z: number = 10) : number {
if(typeof z === 'number') {
return x + y + z
} else {
return x + y
}
}
// 將add函數(shù)賦值給add2
const add2: (x: number, y: number, z?: number) => number = add
類的寫法
修飾符:
public:都可以訪問(讀、改)
private:只能在當(dāng)前類里進(jìn)行訪問匙握,子類和實(shí)例都不能訪問
protected:子類也可以訪問,實(shí)例不能訪問
readonly:只讀
static: 類直接訪問
class Animal {
public name: string;
// 靜態(tài)屬性
static categories: string [] = ['mammal', 'bird']
// 靜態(tài)方法
static isAnimal(a) {
return a instanceof Animal
}
// 構(gòu)造方法
constructor(name: string) {
this.name = name
}
run() {
return `${this.name} is running`
}
}
console.log(Animal.categories) // ['mammal', 'bird']
const snake = new Animal('lily') // lily is running
console.log(Animal.isAnimal(snake)) // true
class Dog extends Animal {
bark() {
return `${this.name} is barking`
}
}
const xiaobao = new Dog('xiaobao')
console.log(xiaobao.run()) // xiaobao is running
console.log(xiaobao.bark()) // xiaobao is barking
class Cat extends Animal {
constructor(name) {
super(name)
console.log(this.name)
}
// 重寫父類方法
run() {
return 'Meow, ' + super.run()
}
}
const miaomiao = new Cat()
console.log(maomao.run()) // miaomiao Meow, maomao is running
接口:interface
interface Person {
readonly id: number;
name: string;
age ?: number;
}
let viking: Person = {
id: 123.
name: 'hebe'
}
對(duì)類的擴(kuò)展
當(dāng)公共屬性和方法不方便抽成一個(gè)類時(shí),可以使用接口進(jìn)行抽象
接口之間也是可以繼承的
interface Radio {
switchRadio() : void;
}
interface Battery {
checkBatteryStatus();
}
interface RadioWithBattey extends Radio {
checkBatteryStatus();
}
class Car implements Radio , Battery {
switchRadio() { };
checkBatteryStatus() { };
}
class Cellphone implements RadioWithBattey {
switchRadio() { };
checkBatteryStatus() { };
}
枚舉
const enum Direction {
Up = 'UP',
Down = 'DOWN',
Left = 'LEFT',
Right = 'RIGHT'
}
// console.log(Direction.Up) // 0
// console.log(Direction[0]) // Up
const value = 'UP'
if (value === Direction.Up) {
console.log('Go Up!')
}
泛型
// 傳入什么類型圈纺,就返回什么類型
function echo<T>(arg: T) : T {
return arg
}
const res = echo(123) // res就是number
const res1 = echo('str') // res1就是string
function swap<T, U>(tuple: [T, U]): [U, T] {
return [tuple[1], tuple[2]]
}
const res2 = swap(['str', 123]) // res2就是number、string的元組
// res2[1]. 就可以使用字符串的方法
// res2[0]. 就可以使用number的方法
// 第一種解決辦法赠堵,對(duì)泛型進(jìn)行限定,但不能根本解決
function echoWithArr<T>(arg: T[]): T[] {
console.log(arg.length)
return arg
}
const arrs = echoWithArr([1, 2, 4])
// 第二種解決方案法褥,結(jié)合接口進(jìn)行限定
interface IWithLength {
length: number
}
function echoWithLength<T extends IWithLength>(arg: T): T {
console.log(arg.length)
return arg
}
const str = echoWithLength('str')
const obj = echoWithLength({ length: 10, width: 10 })
const arr2 = echoWithLength([1, 2, 3])
最后編輯于 :2020.12.13 15:06:50
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者