TypeScript(2)
1.类型
//rename data type type NAME = string const jackName : NAME = 'Jack Jay' //rename mixed data type type PHONE = string | number const jackPhone : PHONE = '123321' const maryPhone : PHONE = 121212 //like interface type Person = { name:string, age:number } const jack : Person = { name:'Jack', age:12 } // cannot rewrite and mixed // throw error type Rewrite = { name:string } type Rewrite = { age:number } // cannot extends but can mixed interface Father { name:string } type Son = Father & { age:number } const john : Son = { name:'', age:12 }
2.元组
//confirm each type of element in array and length const arr : [string,number,boolean] = ['123',123,true] arr[0].split('') arr[1].toFixed(2)
3.类
3.1 public private protected
// public: everyone can use // private: only the class inside which inited it can use // protected: the calss inited and the sub class can use class Person { public name = 'Person' private age = 18 protected phone = 12345 } class Jack extends Person { log() { console.log(this.name) console.log(this.age) console.log(this.phone) } } const jack = new Jack() jack.log()
3.2 class使用数据类型
//must init the value before use constructor //class use data type class Animal { public name constructor(name:string) { this.name = name } }
3.3 readonly
// only can be changed in constructor class Book{ public readonly name constructor(name:string) { this.name = name } }
3.4 abstract
// abstract class can't be inited
// abstract method must be rewrite by sub class
abstract class Animal {
public name
constructor(name:string) {
this.name = name
}
public abstract eat() : any
}
class Dog extends Animal {
public age
constructor(name:string,age:number) {
super(name)
this.age = age
}
public eat() : any {
}
}
const dog = new Dog('123',123)
3.5 interface
// class implements a interface and rewrite interface method
interface Alarm {
alert():void
}
class Door implements Alarm {
alert(){
}
}
class Car implements Alarm {
alert() {
}
}
//interface can extends interface
interface Open {
open():void
}
interface Alarm extends Open {
alert():void
}
class Door implements Alarm {
alert(){
}
open() {}
}
//class extends multiple interface
interface Open {
open():void
}
interface Alarm {
alert():void
}
class Door implements Alarm, Open {
alert(){
}
open() {}
}
//interface extends class
interface Inter extends Name{
a:string
}
class Name {
name: string
age : number
constructor(name:string, age:number) {
this.name = name
this.age = age
}
}
const obj : Inter = {
a:'123',
name:'123',
age:123
}
4.泛型
//when don't konw input type, use <T> instead of data type
// T is customized
function fun<T>(a:T):T{
return a
}
fun(123)
//use more than one
function fun2<T, U>(tuple:[T,U]):[U,T] {
return [tuple[1],tuple[0]]
}
fun2([1,'2'])
// function use parameter varible but don't know data type
// extends interface to add a varible
// while using fun, the parameter must obey the interface rule
interface Length {
length:number
}
function fun<T extends Length>(a:T):T{
console.log(a.length)
return a
}
fun('123')
fun(1)//throw error
// don't konw parameter data type,
interface FUN {
<T>(a:number,b:T):Array<T>
}
let fun: FUN = function<T>(a: number, b: T): Array<T> {
let result: T[] = [];
for (let i = 0; i < a; i++) {
result[i] = b;
}
return result;
}
//or
interface FUN2<T> {
(a:number,b:T):Array<T>
}
let fun2 : FUN2<any> = function<T>(a: number, b: T): Array<T> {
let result: T[] = [];
for (let i = 0; i < a; i++) {
result[i] = b;
}
return result;
}
// calss variable don't know data type
class Person <T> {
value:T
constructor(value:T) {
this.value = value
}
add(a:number,b:T):Array<T>{
return []
}
}
const person = new Person<string>('12')