美文网首页
TS - 接口

TS - 接口

作者: 我是Msorry | 来源:发表于2021-02-12 12:21 被阅读0次

接口是对行为的抽象,由类来定义对象或函数的类型

初始化 tslint 配置规则

tslint --init
//tslint.json
{
    "defaultSeverity": "error",
    "extends": [
        "tslint:recommended"
    ],
    "jsRules": {},
    "rules": {
        "quotemark":[ false],
        "semicolon": [false],
        "interface-name": [true,"never-prefix"],
        "object-literal-sort-keys":[false]
    },
    "rulesDirectory": []
}

可选属性

interface Vegetable{
  color?:string, //可选属性
  type:string
}

const getVegetables = ({color,type}:Vegetable)=>{
  return `A ${color?(color+''):''} ${type}.`
}

console.log(getVegetables({type:'tomato'}))

A red tomato.

绕过多余参数检查

  1. as 断言

告诉 TypeScript 传入的类型就是接口类型

interface Vegetable{
  color?:string, 
  type:string
}

const getVegetables = ({color,type}:Vegetable)=>{
  return `A ${color?(color+''):''} ${type}.`
}

console.log(getVegetables({type:'tomato',color:'red',size:'24'} as Vegetable))
  1. 索引签名
interface Vegetable{
  color?:string, 
  type:string,
  [prop:string]:any
}

const getVegetables = ({color,type}:Vegetable)=>{
  return `A ${color?(color+''):''} ${type}.`
}

console.log(getVegetables({type:'tomato',color:'red',size:'24'}))
  1. 类型兼容性
interface Vegetable{
  color?:string, //可选属性
  type:string,
  [prop:string]:any
}

const getVegetables = ({color,type}:Vegetable)=>{
  return `A ${color?(color+''):''} ${type}.`
}

const vegetableInfo = {type:'tomato',color:'red',size:'24'}

console.log(getVegetables(vegetableInfo))

只读属性

interface ArrInter {
  0: string;
  readonly 1: number;
}

let arr: ArrInter = ['Hello', 18];

arr1[2] = 19//此处报错

函数类型

interface AddFunc {
  (num1: number, num2: number): number
}
/*上下两种写法等价*/

type AddFunc = (num1: number, num2: number) => number


const add: AddFunc = (n1,n2) => n1+n2;

console.log(add(1, 2));

索引类型

interface RoleDic{
  [id:string]:string
}

const role1 = {
  0:'super_admin' //此处不会报错,会自动把数值转化成字符串
}

接口的继承

interface Vegetable {
  color: string
}
interface Tomato extends Vegetable{
  radius:string
}

混合类型接口——函数属性

//JS
let countUp = ()=>{
  countUp.count++
}
countUp.count=0
countUp()
console.log(countUp.count)
countUp()
console.log(countUp.count)
countUp()
console.log(countUp.count)

1
2
3

//TS
interface Counter{
  ():void
  count:number
}

const getCounter = ():Counter=>{
  const c = ()=>{c.count++}
  c.count = 0
  return c
}
const counter:Counter = getCounter()
counter()
console.log(counter.count);
counter()
console.log(counter.count);
counter()
console.log(counter.count);

1
2
3

相关文章

  • 8、TypeScript 接口继承接口,类实现多接口

    1、ts类中只能继承一个父类2、ts类中可以实现多少接口,使用(,)号分隔3、ts接口中可以继承多个接口,使用(,...

  • TS接口

    函数和方法的区别: 当一个函数是对象的属性时,这个函数为该对象的方法 接口就是用代码描述一个对象必须有什么属性(包...

  • TS: 接口

    接口算是官方文档里的第一个知识点了,这篇文章会简单介绍 TypeScrip 里的接口。 类型 在说接口之前我们先来...

  • TS - 接口

    接口是对行为的抽象,由类来定义对象或函数的类型 初始化 tslint 配置规则 可选属性 A red tomato...

  • typeScript语法

    ts类型 ts联合类型使用或 ts定义任意类型any ts定义函数返回值的类型 ts中的类定义 interface接口

  • TypeScript基础知识

    命令 查看版本:tsc -v 运行ts文件:tsc xx.ts 数据类型 接口Interface 类和接口 泛型基...

  • TypeScript 学习笔记 之 接口与类

    接口 TS 中判断是否实现接口的核心原则是基于结构而不是基于名称的。即鸭子类型判断。 TS 中接口可以声明可选的接...

  • 【第7篇】TypeScript泛型的案例代码详解

    1、最简单泛型例子 Ts代码 Js文件 2、泛型类型与接口 Ts代码一 Ts编译js代码一 Ts代码二 Ts编译j...

  • umijs@use-request源码解读

    一、了解ts基本语法 涉及ts的变量声明、接口、类、函数、泛型等 ts语法知识[https://typescrip...

  • ng-alain中的st表格

    后端控制分页的st表格使用 数据列初始化 ts 后端接口获取data ts 表格分页设置 ts change回调 ...

网友评论

      本文标题:TS - 接口

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