当前位置:网站首页>typescript type 和 interface 的区别
typescript type 和 interface 的区别
2022-08-04 03:04:00 【alokka】
1. interface侧重于描述数据结构,type侧重于描述类型
interface A{
name:string; }
type B = 'bb'|'cc'
2. 都可以描述一个对象或者函数
interface user {
name: string;
age: number
}
interface setUser {
(name: string, age: number): void
}
type hoster = {
name: string;
age: number;
}
type setHoster = (name: string, age: number) => void
3. interface 可以定义相同的类型名称,多个相同类型名称会合并,type 不可以
interface Window {
title: string
}
interface Window {
ts: TypeScriptAPI
}
const src = 'const a = "Hello World"';
window.ts.transpileModule(src, {
});
type Window = {
title: string
}
type Window = {
ts: TypeScriptAPI
}
// Error: Duplicate identifier 'Window'.
4. interface 可以继承, type不可以
interface Animal {
name: string
}
interface Bear extends Animal {
honey: boolean
}
const bear = getBear()
bear.name
bear.honey
type Animal = {
name: string
}
type Bear = Animal & {
honey: boolean
}
const bear = getBear();
bear.name;
bear.honey;
5. type专属功能
// type专属 联合类型
interface Dog {
wang()
}
interface Cat {
miao()
}
type Pet = Dog | Cat
type PetList = [Dog,Cat]
边栏推荐
猜你喜欢

千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机

Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED

Pine脚本 | 如何显示和排版绘图开关?

一个属于程序员的七夕节!

在更一般意义上验算移位距离和假设

基于Qt的目录统计QDirStat

Rongyun "Audio and Video Architecture Practice" technical session [complete PPT included]

MallBook联合人民交通出版社,推动驾培领域新发展,开启驾培智慧交易新生态

Sfdp 超级表单开发平台 V6.0.5 正式发布

瑞能微计量芯片RN2026的实用程序
随机推荐
Oracle迁移到瀚高之后,空值问题处理
MySQL查询优化与调优
DIY电工维修如何拆卸和安装开关面板插座
[QNX Hypervisor 2.2 User Manual] 10.3 vdev gic
Architecture of the actual combat camp module three operations
KingbaseES数据库启动失败,报“内存段超过可用内存”
[Playwright Test Tutorial] 5 minutes to get started
【Playwright测试教程】5分钟上手
一文看懂推荐系统:召回05:矩阵补充、最近邻查找,工业界基本不用了,但是有助于理解双塔模型
说说数据治理中常见的20个问题
Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).
pytorch applied to MNIST handwritten font recognition
[Original] Start the XPS/OXPS reader that comes with Windows 10
cdh6.x 集成spark-sql
Utilities of Ruineng Micrometer Chip RN2026
pytorch应用于MNIST手写字体识别
云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
SQL注入中 #、 --+、 --%20、 %23是什么意思?
Big guys, it takes a long time to read mysql3 million single tables, what parameters can be discounted, or is there any way to hurry up
哎,又跟HR在小群吵了一架!