当前位置:网站首页>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]
边栏推荐
- C program compilation and predefined detailed explanation
- QNX Hypervisor 2.2用户手册]10.1 通用vdev选项
- 2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
- 董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
- 瑞能微计量芯片RN2026的实用程序
- Architecture of the actual combat camp module three operations
- How to drop all tables under database in MySQL
- 自制蓝牙手机app控制stm8/stm32/C51板载LED
- sql注入一般流程(附例题)
- KingbaseES数据库启动失败,报“内存段超过可用内存”
猜你喜欢
随机推荐
STM8S105K4T6------Serial port sending and receiving
安装postgis时报找不到“POSTGIS_VERSION”这个函数
ingress 待完善
MRS: Alluxio的使用介绍
In the season of going overseas, the localization of Internet tips for going overseas
逻辑漏洞----其他类型
Exclude_reserved_words 排除关键字
MallBook联合人民交通出版社,推动驾培领域新发展,开启驾培智慧交易新生态
TOML configuration file format, YAML's top contender
Day13 Postman的使用
Deep Learning (3) Classification Theory Part
keytool命令
Why use Selenium for automated testing
Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书
View mysql deadlock syntax
golang中的unsafe.Pointer,指针,引用
sqoop ETL工具
怎样提高网络数据安全性
STM8S-----选项字节









