網站首頁 編程語言 正文
interface VS type
大家使用 typescript 總會使用到 interface 和 type,官方規范 稍微說了下兩者的區別
- 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.
但是沒有太具體的例子。
明人不說暗話,直接上區別。
相同點
都可以描述一個對象或者函數
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 都可以拓展,并且兩者并不是相互獨立的,也就是說 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;
}
不同點
type 可以而 interface 不行
- type 可以聲明基本類型別名,聯合類型,元組等類型
// 基本類型別名
type Name = string
// 聯合類型
interface Dog {
wong();
}
interface Cat {
miao();
}
type Pet = Dog | Cat
// 具體定義數組每個位置的類型
type PetList = [Dog, Pet]
- type 語句中還可以使用 typeof 獲取實例的 類型進行賦值
// 當你想獲取一個變量的類型時,使用 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
}
*/
總結
一般來說,如果不清楚什么時候用interface/type,能用 interface 實現,就用 interface , 如果不能就用 type 。其他更多詳情參看 官方規范文檔
原文鏈接:https://juejin.cn/post/6844903749501059085
相關推薦
- 2022-11-30 React18?中的?Suspense?API使用實例詳解_React
- 2023-03-13 React?createRef循環動態賦值ref問題_React
- 2022-04-08 IIS部署ASP.NET?Core項目及常見問題總結_基礎應用
- 2023-03-17 Go語言依賴管理三要素示例解析_Golang
- 2022-11-01 R語言在散點圖中添加lm線性回歸公式的問題_R語言
- 2022-05-16 Qt5中QML自定義環形菜單/環形選擇框的實現_C 語言
- 2022-09-10 關于pandas.date_range()的用法及說明_python
- 2022-12-31 Android入門之Service的使用詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支