typeScript 基础点

2022/8/8 23:25:47

本文主要是介绍typeScript 基础点,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

// ts 数据类型

// 1. 基本数据类型: string, number, boolean, undefined, null
let s:string = 'aaaa'

// 2. 数组: number[]   泛型写法:Array<number>
let arr:number[] = [1,2,3]
let arr1:Array<string> = ['1', '2', '3']

//3. 元组:[string, number, boolean] 定义和复制一一对应
let temp:[string, number, boolean] = ['a', 12, true]

//4. 枚举 enum Color{red, green, blue}
console.log(Color[1]) // green
console.log(Color['green']) // 1

//5. any
//6. void
//7. object

//8. 联合类型  string|number  可以是数字或者number

//9.1 接口 interface 属性和值的约束
interface Iperson {
    readonly id: number, // readonly 只读
    age?: number,        // 可选
    name: string
}

const person = {
    id: 1,
    age: 20,
    name: '法外狂徒'
}

//9.2 接口实现函数调用签名
interface ISearch {
    (source:string, substr: string):boolean
}

var searching:ISearch = (source:string, substr: string):boolean => {
    return source.search(substr)
}

//9.3 接口实现类的约束,类通过implements实现多个接口逗号隔开, 接口可以通过 extends 继承其他接口
interface ISwim {
  swim():void
}
class Person implements ISwim {
    swim() {
        console.log('swiming................')
    }
}


//10 修饰符
// 1. public     可访问:子类+实例
// 2. private    可访问:自己
// 3. protected  可访问:子类
// 4. readonly   可访问:修饰的属性,只能在构造函数中初始化,自己的方法和外部实例以及子类都不能修改

//11 存取器 get set

//12 静态成员 static  只能类名.属性访问
//13 抽象类,抽象方法  abstract 修饰, 子类实现父类 且实现其父类的抽象方法
//14 函数的默认参数(有默认值),可选参数(后面加 ?),剩余参数(最后一个参数, ...args: [])
//15 泛型 <T>:定义的时候不知道类型,使用时才能确定类型。
// 1. 泛型函数 getNum<K, V>(v1: K, v2:V): [K, V] {return [v1, v2]}
// 2. 泛型接口

 



这篇关于typeScript 基础点的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程