当前位置:网站首页>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]
边栏推荐
- 三分建设,七分管理!产品、系统、组织三管齐下节能降耗
- Day13 Postman的使用
- Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
- QNX Hypervisor 2.2用户手册]10.1 通用vdev选项
- 云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
- KingbaseES数据库启动失败,报“内存段超过可用内存”
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
- 查看mysql死锁语法
- Why use Selenium for automated testing
- 第08章 索引的创建与设计原则【2.索引及调优篇】【MySQL高级】
猜你喜欢
Brush esp8266-01 s firmware steps
【项目实现】Boost搜索引擎
董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).
【学习笔记之菜Dog学C】动态内存管理
小程序:扫码打开参数解析
cdh6.x 集成spark-sql
STM8S105k4t6c---------------Light up LED
[Study Notes Dish Dog Learning C] Dynamic Memory Management
DIY电工维修如何拆卸和安装开关面板插座
随机推荐
LeetCode每日一题(2285. Maximum Total Importance of Roads)
【医保科普】维护医保基金安全,我们可以这样做
2022年最新海南建筑八大员(材料员)模拟考试试题及答案
第08章 索引的创建与设计原则【2.索引及调优篇】【MySQL高级】
云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
QNX Hypervisor 2.2用户手册]10.2 vdev 8259
织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
DIY电工维修如何拆卸和安装开关面板插座
出海季,互联网出海锦囊之本地化
哎,又跟HR在小群吵了一架!
Presto中broadcast join和partition join执行计划的处理过程
一个属于程序员的七夕节!
QNX Hypervisor 2.2用户手册]10.1 通用vdev选项
ingress 待完善
单片机C语言->的用法,和意思
web端动效 lottie-web 使用
如果禁用了安全启动,GNOME 就会发出警告
[QNX Hypervisor 2.2 User Manual] 10.3 vdev gic