typescript 接口篇

(小白学习之接口篇,请多指教)

ts接口主要用来定义一个对象的类型,通过interface关键字来定义

举个🌰:


interface Person {
  name: string;
  age: number
}

const xiong: Person = {
  name: '熊',
  age: 18
}

属性可选:

有时候我们不确定对象的某个属性是否存在,这时候就需要将该属性设置成可选属性


interface Person {
  name: string;
  age: number;
  money?: number;
}

const xiong: Person = {
  name: '熊',
  age: 18,
}

const xiongxiong: Person = {
  name: '熊',
  age: 18,
  money:999999
}

只读属性:

字面上的意思,如果某个属性的类型定义为只读,那么改接口的变量不能修改该属性,举个🌰:


interface Person {
  name: string;
  readonly age: number;
}

const xiong: Person = {
  name: '熊',
  age: 18,
}

console.log(xiong.age) // 18
xiong.age = 20 //报错

混合类型:

当一个函数既可以直接调用,又拥有一些属性的时候,就需要使用interface定义函数类型


interface Func {
  (name: string): string;
  age: number;
}

const myFunc = function(name: string) {
 return name;
};
myFunc.age = 18;
const func: Func = myFunc;
console.log(func);

类型索引:

当定义数组或者不确定属性的对象时,可以使用类型索引


interface ObjType {
 [value: string]: string;
}

const object: ObjType = {
 name: "xiong",
 like: "bear"
};
console.log(object); //{name: "xiong", like: "bear"}

// 如果索引是number类型 那么该类型也可以作为数组
interface ArrType {
 [value: number]: string;
}

const obj: ArrType = {
 1: "xiong",
 2: "bear"
};

const array: ArrType = ["xiong", "bear"];
console.log(array); // ["xiong", "bear"]
console.log(obj); // {1: "xiong", 2: "bear"}

一旦定义了类型索引,那么再定义具名属性类型必须与索引类型一致

interface AnyObj {
 [name: string]: string;
 gender: string; //pass
 // age: number; // error age的值类型需要时string
}

interface Any {
 [name: string]: string | number;
 // [name: any]: any; //error 索引签名参数类型必须为'string'或'number'
 // 或者可以定义为[name: string]: string | number;
 gender: string; //pass
 age: number; // error age的值类型需要时string
}

类类型:

interface除了能约束普通变量,也可以约束类,让类按照约定来实现

举个简单🌰:


interface Person {
  name: string;
  age: number;
}

//通过implements关键字来约束xiong按照Person类型来实现
class xiong implements Person {
  name = 'xiong'
  age = 18
}

也可以约束类型中的方法,举个🌰:


interface Person {
  getMoney(num:number):number
}

class xiong implements Person {
  getMoney(num) {
    return num*2
  }
}

接口继承:

接口和class一样,可以继承

举个简单🌰:


interface Person {
  name: string;
  age: number;
}

interface Teacher extends Person {
  job: string  
}

也可以继承多个接口,🌰:


interface Person {
  name: string;
  age: number;
}

interface Teacher {
  book: string;
}

interface Xiong extends Person, Teacher {
  job: string;
}

const XiongXiong: Xiong = {
  name: "xiongxiong",
  age: 18,
  job: "it",
  book: "qqq"
};
console.log(XiongXiong); //{name: "xiongxiong", age: 18, job: "it", book: "qqq"}

一个接口也可以继承一个class ,🌰:


class Action {
  run = true;
}
interface Movement extends Action {
  sit: boolean;
}
const man: Movement = {
  run: true,
  sit: true
};
console.log(man);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 类 介绍 传统的JavaScript程序使用函数和基于原型的继承来创建面向对象的类,在es6中,JavaScrip...
    大脸猫的前端之路阅读 300评论 0 0
  • 日期:2019 年 8 月 29 日 typescript 接口 介绍 TypeScript的核心原则之一是对值所...
    五十岚色叶阅读 428评论 0 2
  • TypeScript的核心原则之一是对值所具有的结构进行类型检查。 它有时被称做“鸭式辨型法”或“结构性子类型化”...
    2o壹9阅读 643评论 0 48
  • 接口 TypeScript 的核心原则之一是对值所具有的结构进行类型检查。它有时被称做“鸭式辨型法”或“结构性子类...
    罗彬727阅读 292评论 0 0
  • 介绍 TypeScript 的核心原则之一是对值所具有的结构进行类型检查。它有时被称做 鸭式辨型法 或 结构性子类...
    24KBING阅读 234评论 0 0