網(wǎng)站首頁 編程語言 正文
Typescript中interface與type的相同點(diǎn)與不同點(diǎn)的詳細(xì)說明_基礎(chǔ)知識
作者:七月流螢 ? 更新時(shí)間: 2022-12-24 編程語言interface VS type
大家使用 typescript 總會(huì)使用到 interface 和 type,官方規(guī)范 稍微說了下兩者的區(qū)別
- An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
- An interface can have multiple merged declarations, but a type alias for an object type literal cannot.
但是沒有太具體的例子。
明人不說暗話,直接上區(qū)別。
相同點(diǎn)
都可以描述一個(gè)對象或者函數(shù)
interface
interface User {
name: string
age: number
}
interface SetUser {
(name: string, age: number): void;
}
type
type User = {
name: string
age: number
};
type SetUser = (name: string, age: number)=> void;
都允許拓展(extends)
interface 和 type 都可以拓展,并且兩者并不是相互獨(dú)立的,也就是說 interface 可以 extends type, type 也可以 extends interface 。 雖然效果差不多,但是兩者語法不同。
interface extends interface
interface Name {
name: string;
}
interface User extends Name {
age: number;
}
type extends type
type Name = {
name: string;
}
type User = Name & { age: number };
interface extends type
type Name = {
name: string;
}
interface User extends Name {
age: number;
}
type extends interface
interface Name {
name: string;
}
type User = Name & {
age: number;
}
不同點(diǎn)
type 可以而 interface 不行
- type 可以聲明基本類型別名,聯(lián)合類型,元組等類型
// 基本類型別名
type Name = string
// 聯(lián)合類型
interface Dog {
wong();
}
interface Cat {
miao();
}
type Pet = Dog | Cat
// 具體定義數(shù)組每個(gè)位置的類型
type PetList = [Dog, Pet]
- type 語句中還可以使用 typeof 獲取實(shí)例的 類型進(jìn)行賦值
// 當(dāng)你想獲取一個(gè)變量的類型時(shí),使用 typeof
let div = document.createElement('div');
type B = typeof div
- 其他騷操作
type StringOrNumber = string | number;
type Text = string | { text: string };
type NameLookup = Dictionary<string, Person>;
type Callback<T> = (data: T) => void;
type Pair<T> = [T, T];
type Coordinates = Pair<number>;
type Tree<T> = T | { left: Tree<T>, right: Tree<T> };
interface 可以而 type 不行
interface 能夠聲明合并
interface User {
name: string
age: number
}
interface User {
sex: string
}
/*
User 接口為 {
name: string
age: number
sex: string
}
*/
總結(jié)
一般來說,如果不清楚什么時(shí)候用interface/type,能用 interface 實(shí)現(xiàn),就用 interface , 如果不能就用 type 。其他更多詳情參看 官方規(guī)范文檔
原文鏈接:https://juejin.cn/post/6844903749501059085
相關(guān)推薦
- 2023-01-07 Android開發(fā)X?Y軸Board的繪制教程示例_Android
- 2022-07-15 C++?DLL動(dòng)態(tài)庫的創(chuàng)建與調(diào)用(類庫,隱式調(diào)用)_C 語言
- 2022-12-09 C++哈希表之閉散列方法的模擬實(shí)現(xiàn)詳解_C 語言
- 2022-12-04 詳解Go?依賴管理?go?mod?tidy_Golang
- 2022-11-14 UNIX環(huán)境高級編程筆記
- 2023-07-22 IntelliJ Compilation Error zip END header not foun
- 2022-10-28 Kotlin?嵌套函數(shù)開發(fā)技巧詳解_Android
- 2022-07-04 C#字符串String及字符Char的相關(guān)方法_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支