首页 > 其他分享 >typeScript学习-interface和type 区别

typeScript学习-interface和type 区别

时间:2023-08-16 17:11:57浏览次数:42  
标签:brandNo typeScript string 接口 类型 interface type

typeScript学习

interface(接口) 和 type 区别

type 和接口类似,都用来定义类型,但 type 和 interface 区别如下:

区别1:定义类型范围不同

interface 只能定义对象类型或接口当名字的函数类型。

type 可以定义任何类型,包括基础类型、联合类型、交叉类型,元组。

// type 定义基础类型
type num = number

// type 定义联合类型-示例1
type baseType = string | number | symbol

// type 定义联合类型-示例2
interface Car { brandNo: String }
interface Plane { No: string, brandNo: String }

type TypVechile = Car | Plane


let vechile1: TypVechile = { brandNo: "奔驰" }
let vechile2: TypVechile = { brandNo: "宝马", No: "AB1001" }

// 元组
interface Car2 { brandNo: String }
interface Plane2 { No: string, brandNo: String }

type TypVechile2 = [Car2, Plane2]

 

区别2:

接口可以 extends(继承) 一个或多个接口或类实现一个或多个接口,也可以继承 type,但 type 类型没有继承功能,

但一版接口继承 类 和 type 的应用场景很少见。

区别3:

用 type 交叉类型 & 可让类型中的成员合并成一个新的 type 类型,但接口不能交叉合并

type Group = { groupName: string, memberNum: number }
type GroupInfoLog = { info: string, happen: string }
type GroupMemeber = Group & GroupInfoLog // type 交叉类型

let data: GroupMemeber = {
    groupName: '001',
    memberNum: 10,
    info: '爬山',
    happen: '有惊无险'
}

export { }

 

区别4:接口可合并声明

定义两个相同名称的接口会合并声明,定义两个同名的 type 会出现编译错误。

interface Error {
    name: string
}

interface Error {
    message: string,
    stack?: string
}

// 接口合并

let error: Error = {
    message: '空指针',
    name: 'NullPointException'
}

 

标签:brandNo,typeScript,string,接口,类型,interface,type
From: https://www.cnblogs.com/kongbaige/p/17635529.html

相关文章

  • 报错TypeError: Cannot read properties of null (reading 'length')
    可能是某个数组属性不存在,但是判断了他的长度,比如下方代码<templatev-if="arr.length"><divv-for="(item,idx)inarr":key="idx">{{item}}</div></template>解决方法  v-if="arr......
  • Grafana日志转换-Convert field type & Concatenate fields & Organize fields
    Grafana日志转换-Convertfieldtype&Concatenatefields&Organizefields转换函数根据自己的使用情况,记录使用到的转换函数,这次涉及到的转换函数如下Convertfieldtype功能:转换字段的类型Concatenatefields功能:将所有字段合并为一个数据集合。如果长度不相同,值将附加......
  • NetSuite: Get specific Custom Record Types and related sub Custom Fields
    背景以前当使用search.create({})来获取数据时,我们需要制定特定的数据返回列;例如:search.createColumn(options)而query可以使用SELECT*FROM来动态返回所有的数据列(这在有的时候是一个优点),那么如何让search也动态返回所有的数据列呢?vararrColFlds=query.runSuiteQL({......
  • 【FPGA】 DDR3读写(基于User Interface)
    【FPGA】DDR3读写(基于UserInterface)DDR3概述DDR3(doubledatarate3synchronousdynamicRAM)第三代双倍数据速率同步动态随机存储器同步:数据的速去和写入时钟同步动态:数据掉电无法保存,需要周期性刷新才能保持数据随机存取:能够对任意地址进行操作双倍数据速率:时钟的......
  • Unveiling the Power of Diagnostic Tools: Cat Caterpillar ET, Volvo Vocom II Inte
    Intherealmofautomotivediagnostics,therighttoolscanmakeallthedifference.Whetheryou'reaprofessionalmechanicoranavidcarenthusiast,havingaccesstoreliablediagnostictoolsiscrucial.Inthisblogpost,wewillexplorethecapab......
  • Mybatis中的resultType和resultMap
    综述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接返回设置的类型,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面......
  • 8-15| _ctypes.COMError: (-2147352567, '发生意外。', ('无法获取 Document 对象', '
    此错误是一个COM错误,它与试图从Python通过`pyautocad`与AutoCAD通信时出现的问题有关。错误信息"无法获取Document对象"指示了问题的本质,即Python无法访问AutoCAD的当前文档。这里有一些建议来解决这个问题:1.**确保AutoCAD已经运行**:在尝试从Python访问Aut......
  • 参考文献列表:Mixed-type conversation
    TowardsTopic-GuidedConversationalRecommenderSystemRecInDial:AUnifiedFrameworkforConversationalRecommendationwithPretrainedLanguageModelsRLPROMPT:OptimizingDiscreteTextPromptswithReinforcementLearningCodeRL:MasteringCodeGeneratio......
  • MyBatis-Plus中IdType策略不生效的问题
    问题背景在开发过程中,我们经常会使用MyBatis-Plus来简化与数据库的交互,其中IdType是一个重要的配置项,用于指定实体类中主键的生成策略。然而,在使用Mapper文件进行插入操作时,有时候会遇到IdType策略不生效的情况,这引发了我对问题的深入探索。今天在修改公司接口时,顺手测试了......
  • 前端周刊第66期:TypeScript教程、ESM、React泡沫、htmx、测试文章
    周刊同步发表于微信公众号“写代码的宝哥”,欢迎各位小伙伴前来关注......