五、接口
// 接口 约束对象结构 必须遵循接口规范 // 可选成员 只读成员 动态成员 export {} interface Post { title: string content: string subtitle?: string // 可选成员 readonly summary: string // 只读成员 } function printPost (post: Post) { console.log(post.title) console.log(post.content) } printPost({ title: 'typescript', content: 'a surperset of js', summary: 'a description' }) // -------------------------- // 动态成员 interface Cache { [key: string]: string } const cache: Cache = {} cache.foo = '123' cache.bar = '456'
六、类
// 类 export {} class Person { // 定义对象实例中属性的类型 name: string // = 'init name' age: number constructor (name: string, age: number) { this.name = name this.age = age } sayHi (msg: string): void { console.log(`I am ${this.name}, ${msg}.`) } }
1.类中的修饰器
// 类 访问修饰符 export {} class Person { // 访问修饰符: public 公有属性 public name: string // = 'init name' // 访问修饰符: private 私有属性 (不能在外部访问) private age: number // 访问修饰符: protected 被保护的属性 (不能在外部访问,但可以在子类访问) protected gender: boolean constructor (name: string, age: number) { this.name = name this.age = age this.gender = true } sayHi (msg: string): void { console.log(`I am ${this.name}, ${msg}.`) console.log(this.age) } } class Student extends Person { // 私有类 private constructor (name: string, age: number) { super(name, age) console.log(this.gender) // gender 可以在子类访问到 } static createObj (name: string, age: number) { return new Student(name, age); } } const mike = new Person('mike', 18); console.log(mike.name) // console.log(mike.age) // console.log(mike.gender) const jack = Student.createObj('jack', 20);
2.只读属性
// 类的只读类型 export {} class Person { public name: string // = 'init name' private age: number // 只读属性 readonly protected readonly gender: boolean constructor (name: string, age: number) { this.name = name this.age = age this.gender = true } sayHi (msg: string): void { console.log(`I am ${this.name}, ${msg}.`) console.log(this.age) } } const mike = new Person('mike', 18); console.log(mike.name)
3.类与接口
// 类 与 接口 export {} interface Eat { eat (food: string): void } interface Run { run (distance: number): void } class Human implements Eat, Run { eat (food: string): void { console.log(`吃了${food}`) } run (distance: number): void { console.log(`走了${distance}`) } } class Animal implements Eat, Run { eat (food: string): void { console.log(`进食${food}`) } run (distance: number): void { console.log(`爬了${distance}`) } }
4.抽象类
// 抽象类 export {} abstract class Animal { eat (food: string): void { console.log('吃吃吃', food) } abstract run (distance: number): void } class Dog extends Animal { run (distance: number): void { console.log('走一走', distance) } } const d = new Dog(); d.eat('cakes'); d.run(100);
七、泛型
// 泛型 在声明时不去指定声明类型,等到调用时再去传递具体的类型 export {} function createNumberArray (length: number, value: number):number[] { const arr = Array<number>(length).fill(value) return arr } function createStringArray (length: number, value: string):string[] { const arr = Array<string>(length).fill(value) return arr } // 泛型 function createArray<T> (length: number, value: T): T[] { const arr = Array<T>(length).fill(value) return arr } const res = createNumberArray(3, 100) // [100, 100, 100] const res2 = createArray<string>(3, '100') // ['100', '100', '100']
八、类型声明
// 类型声明 import { cameCase } from 'lodash' // 声明函数类型 declare function cameCase(input: string):string const res = cameCase('typed javascript') export {}
如果不写类型声明,那么用 npm i --save-dev @types/lodash
来下载官方提供的 ts 类型声明文件!