当前位置:网站首页>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]
边栏推荐
- QNX Hypervisor 2.2 user manual] 10.1 gm vdev options
- 一文看懂推荐系统:召回05:矩阵补充、最近邻查找,工业界基本不用了,但是有助于理解双塔模型
- Detailed analysis of scaffolding content
- MCU C language -> usage, and meaning
- 创新互融|华秋赋能助力OpenHarmony生态硬件开发落地
- 瑞能微计量芯片RN2026的实用程序
- [Study Notes Dish Dog Learning C] Dynamic Memory Management
- [Original] Start the XPS/OXPS reader that comes with Windows 10
- How to read the resources files in the directory path?
- Pine Script | How to display and typeset a plot switch?
猜你喜欢
随机推荐
Exclude_reserved_words 排除关键字
MallBook联合人民交通出版社,推动驾培领域新发展,开启驾培智慧交易新生态
What is the source of flinkcdc consuming mysql binlog data without sqltype=delete
从图文展示到以云为核,第五代验证码独有的策略情报能力
new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
三分建设,七分管理!产品、系统、组织三管齐下节能降耗
验证码业务逻辑漏洞
C语言--环形缓存区
Zabbix设置邮件告警+企业微信告警
织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
2022广东省安全员A证第三批(主要负责人)考试题库及模拟考试
【云原生】DevOps(六):Jenkins流水线
Presto中broadcast join和partition join执行计划的处理过程
Returns the maximum number of palindromes in a string
在更一般意义上验算移位距离和假设
JVM内存和垃圾回收-07.堆
QNX Hypervisor] 10.2 vdev 8259 2.2 user manual
QNX Hypervisor 2.2用户手册]10.1 通用vdev选项
C# 构造函数业务场景测试项目
2022年茶艺师(中级)考试试题模拟考试平台操作