美文网首页
TS typescript 接口 interface

TS typescript 接口 interface

作者: 江火渔枫 | 来源:发表于2022-08-29 16:11 被阅读0次

接口之间叫继承(extends) 类和接口之间叫实现(implements)

基础定义接口

interface IPerson {
 // id 只读属性
readonly id : number
// name、age 必须有
name: string
age: number
// sex 可有可无 加?
sex?: string
}

常用接口定义方法

interface IType {
  [key: string]: string;
}

interface Form {
  "username": string,
  "nickName": string,
  "email": string,
  "password": string,
  "phone": string,
  "companyName": string,
  "position": string,
  // 定义一个数组,数组中元素为 对象
  "roles": IType[],
  // 通用类型设计
  [key: string]: any;
}
const ruleForm: Form = reactive({
  "username": "",
  "email": "",
  "nickName": "",
  "password": "",
  "phone": "",
  "position": "",
  "companyName": "",
  "roles": [],
});
//通过接口限定函数类型
interface ISrearchFun {
  (source: string, subString: string): boolean
}
//实现
const search: ISrearchFun = function (source: string, subString: string): boolean{
  return source.search(subString) > 1
}
// 调用
console.log('今天星期四','四')

类的类型

interface IFly{
  fly()
}
// 定义类 实现上面接口
class Person implements IFly{
  // 必须有接口中的方法
    fly(){
    console.log('我飞了!');
  }
}
// 实例化
const person = new Person();
person.fly();


// 实现多个接口
interface ISwim{
  swim()
}
class Person2 implements IFly,ISwim{
  // 必须有接口中的方法
    fly(){
    console.log('我飞了!');
  }
    swim(){
    console.log('我游泳!');
  }
}
// 实例化
const person2 = new Person2();
person2.fly();
person2.swim();

// 将多类型整合  一次实现
class Sports implements IFly,ISwim{ }
class Person3 implements Sports {
  // 必须有接口中的方法
    fly(){
    console.log('我飞了!');
  }
    swim(){
    console.log('我游泳!');
  }
}
const person3 = new Person3();
person3.fly();
person3.swim();

相关文章

  • TS typescript 接口 interface

    接口之间叫继承(extends) 类和接口之间叫实现(implements) 基础定义接口 常用接口定义方法 类的类型

  • 学习TypeScript 接口

    TypeScript 接口定义 interface interface_name {} 实例 联合类型和接口 接口...

  • angluar学习笔记基本操作

    新建 typescript interface笔记app.component.ts app.component.h...

  • TypeScript 中的接口(interface)

    TypeScript 中的接口可分为: 之前的文章 TypeScript 基础类型和接口(interface)里面...

  • ts接口 interface

    概念:可以用来约束一个函数,对象,以及类的结构和类型 1.对象类型的接口 2.函数类型的接口 3.混合类型的接口(...

  • TypeScript基础语法 - interface 接口(二)

    TypeScript 中的 interface 接口 Interface接口初步了解 现在我们要作一个简历的自动筛...

  • TypeScript接口(interface)

    介绍 TypeScript的核心原则之一是对值所具有的结构进行类型检查。 它有时被称做“鸭式辨型法”或“结构性子类...

  • typeScript接口interface

    什么是接口 接口是一系列抽象方法的声明,是一些方法特征的集合,这些方法都应该是抽象的,需要由具体的类去实现,然后第...

  • typescript 接口interface

    TypeScript的核心原则之一是对值所具有的结构进行类型检查。 可选属性: ? 只读属性: readonly ...

  • TypeScript - 接口

    TypeScript - 接口( Interface) [TOC] 学习目标 理解接口的概念 学会通过接口标注复杂...

网友评论

      本文标题:TS typescript 接口 interface

      本文链接:https://www.haomeiwen.com/subject/yzmhnrtx.html