首页 > 其他分享 >TS中的Type和Interface

TS中的Type和Interface

时间:2023-08-01 15:14:32浏览次数:29  
标签:string Type number TS interface Interface type

基本介绍

学习内容来源于:https://www.youtube.com/watch?v=Idf0zh9f3qQ

Type 与 Interface的区别

  1. 编写方式
  2. 继承方式

Type 的优势

  1. Interface只能描述对象,而Type还可以描述其他类型如stringnumberboolean
  2. Type可以描述联合类型和Interface不行
  3. Type在使用Utility Types时更简洁
  4. Type在使用Tuples时更简洁
  5. Type可以从其他地方直接抽取类型
  6. Interface会自动合并,而Type不会

Type 与 Interface的区别

  1. 编写方式
type UserProps = {
	name: string;
	age: number;
}

interface UserProps {
	name: string;
	age: number;
}
  1. 继承方式
// type 写法
type UserProps = {
	name: string;
	age: number;
}
type AdminProps = UserProps & {
	role: string
}

// interface 写法
interface UserProps {
	name: string;
	age: number;
}
interface AdminProps extends UserProps {
	role: string
}

Type 相较于 Interface的优势

  1. Interface只能描述对象,而Type还可以描述其他类型如stringnumberboolean
type Address = string
  1. Type可以描述联合类型和Interface不行
type Address = string | string[]
  1. Type在使用Utility Types时更简洁
type UserProps = {
	name: string;
	age: number;
	createAt : Date
}
// 在UserProps的基础上去掉createAt
type GuestProps = Omit<UserProps, "createAt">
// 如果删除多个
type GuestProps = Omit<UserProps, "name" | "age">


// 如果使用interface,将会是:
interface GuestProps extends Omit<UserProps, "name" | "age">

Utility Types文档

  1. Type在使用Tuples时更简洁
type Address = [string, number]

// 如果使用interface,将会是:
interface Address extends Array<number | string>{
	0: number;
	1: string
}
  1. Type可以从其他地方直接抽取类型
const project = {
	title: "Project 1",
	specification: {
		areaSize: 100,
		rooms: 3
	}
}

// 抽取全部类型
type Specification = typeof project
// --- 等价于:
type Specification = {
	title: string;
	specification: {
		areaSize: number;
		rooms: number;
	}
}

// 抽取里边的specification类型
type Specification = typeof project["specification"]
// --- 等价于:
type Specification = {
	areaSize: number;
	rooms: number;
}
  1. Interface会自动合并,而Type不会
interface User{
	name: string;
	age: number;
}

interface User{
	role: string;
}

let user:User

// Type不允许声明两个同名的type

标签:string,Type,number,TS,interface,Interface,type
From: https://www.cnblogs.com/sanhuamao/p/17596548.html

相关文章

  • After Effects 2023 - 视频特效和动画制作软件mac/win版
    AfterEffects2023是一款功能强大的视频合成和特效制作软件,它被广泛用于电影、电视、广告和网络视频等领域。在这800字的介绍中,我将向您详细说明AfterEffects2023的特点、功能和优势。点击获取AfterEffects2023 首先,AfterEffects2023提供了一个直观而强大的界面,使......
  • requests详细参数说明
    -1.timeout:请求超时时间,单位为秒。   ```python importrequests   response=requests.get(url,timeout=5)#设置5秒超时 ```   2.verify:请求SSL证书验证。   ```python importrequests   response=requests.get(url,verif......
  • requests-html基础使用
    Requests-HTML是一个基于Python的库,它是在Requests库的基础上构建的,并使用了PyQuery库来实现HTML解析。它提供了一个简单的方法来解析HTML文档并提取信息。 以下是使用Requests-HTML的步骤: 1.安装Requests-HTML库:`pipinstallrequests-html` 2.导入RequestsHTML库:`fro......
  • requests-html高级用法
    以下是一些Requests-HTML的高级用法: 1.使用`Session()`对象: 如果要保持会话状态并在多个请求之间共享Cookie和其他信息,可以使用`Session()`对象: ``` session=HTMLSession() r=session.get('https://www.example.com') #dosomething r=session.get('......
  • netfilter中有哪些挂载点(hook points)?
    5个挂载点,挂载点有各种数据包处理的规则。 分别是:PREROUTINGINPUTFORWAROUTPUTPOSTROUTING 这些挂接点(hookpoints),是数据包处理的不同的阶段。 每个挂节点上,都可以挂载不同的包的处理规则,从而实现对数据包的处理和过滤。 ......
  • [React] forwardRef typescript
    importReact,{forwardRef}from"react";//DeclareatypethatworkswithgenericcomponentstypeFixedForwardRef=<T,P={}>(render:(props:P,ref:React.Ref<T>)=>React.ReactElement)=>(props:P&React.RefAttri......
  • Ubuntu22.04LTS安装oh-my-cap
    使用oh-my-cap需要安装如下软件sac、taup、gmt,如需使用seed数据还需安装rdseed。下载oh-my-cap-2.0.0安装包并解压安装$tar-xvfoh-my-cap-2.0.0$mvoh-my-cap-2.0.0~/opt/oh-my-cap#安装fk$cd~/opt/oh-my-cap/src/fk$make#安装gcap$cd~/opt/oh-my-cap/src/g......
  • echarts中legend实现排列对齐
    问题当图表中的legend过多的时候,就需要考虑legend进行换行,但是换行之后,图例就会无法对齐。解决legend:{icon:"rect",width:"80%",itemWidth:6,itemHeight:6,bottom:0,textStyle:{color:"#333",rich:{a:{width:100,......
  • 爬虫学习(一)——requests库
    一、安装cmd-->pipinstallrequests安装测试>>>importrequests>>>r=requests.get("http://www.baidu.com")>>>r.status_code200>>>r.text 二、Requests库的get()方法r=requests.get(url)返回一个包含服务器的资源的Response对象,构造一......
  • 微信小程序使用echarts动态设置宽高
    微信小程序中动态设置了echarts的高度,但是canvas变形,并没有重新resize原因chart获取不到父组件的宽高,小程序里获取宽高用的是wx.createSelectorQuery(),echarts里resize的时候,并没有调用这个API,肯定无法重置宽高。解决if(chart){constgetWindowInfo=uni.getWindowInf......