成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

TypeScript 中提升幸福感的 10 個高級技巧

開發 前端
用了一年時間的 TypeScript 了,項目中用到的技術是 Vue + TypeScript 的,深感中大型項目中 TypeScript 的必要性,特別是生命周期比較長的大型項目中更應該使用 TypeScript。

用了一年時間的 TypeScript 了,項目中用到的技術是 Vue + TypeScript 的,深感中大型項目中 TypeScript 的必要性,特別是生命周期比較長的大型項目中更應該使用 TypeScript。

以下是我在工作中總結到的經常會用到的 TypeScript 技巧。

1. 注釋

通過 /** */ 形式的注釋可以給 TS 類型做標記提示,編輯器會有更好的提示:

  1. /** This is a cool guy. */ 
  2. interface Person { 
  3.   /** This is name. */ 
  4.   name: string, 
  5.  
  6. const p: Person = { 
  7.     name: 'cool' 

如果想給某個屬性添加注釋說明或者友好提示,這種是很好的方式了。

 

2. 接口繼承

和類一樣,接口也可以相互繼承。

這讓我們能夠從一個接口里復制成員到另一個接口里,可以更靈活地將接口分割到可重用的模塊里。

  1. interface Shape { 
  2.     color: string; 
  3.  
  4. interface Square extends Shape { 
  5.     sideLength: number; 
  6.  
  7. let square = <Square>{}; 
  8. square.color = "blue"
  9. square.sideLength = 10

一個接口可以繼承多個接口,創建出多個接口的合成接口。

  1. interface Shape { 
  2.     color: string; 
  3.  
  4. interface PenStroke { 
  5.     penWidth: number; 
  6.  
  7. interface Square extends Shape, PenStroke { 
  8.     sideLength: number; 
  9.  
  10. let square = <Square>{}; 
  11. square.color = "blue"
  12. square.sideLength = 10
  13. square.penWidth = 5.0

3. interface & type

TypeScript 中定義類型的兩種方式:接口(interface)和 類型別名(type alias)。

比如下面的 Interface 和 Type alias 的例子中,除了語法,意思是一樣的:

Interface

  1. interface Point { 
  2.   x: number; 
  3.   y: number; 
  4.  
  5. interface SetPoint { 
  6.   (x: number, y: number): void

Type alias

  1. type Point = { 
  2.   x: number; 
  3.   y: number; 
  4. }; 
  5.  
  6. type SetPoint = (x: number, y: number) => void

而且兩者都可以擴展,但是語法有所不同。此外,請注意,接口和類型別名不是互斥的。接口可以擴展類型別名,反之亦然。

Interface extends interface

  1. interface PartialPointX { x: number; } 
  2. interface Point extends PartialPointX { y: number; } 

Type alias extends type alias

  1. type PartialPointX = { x: number; }; 
  2. type Point = PartialPointX & { y: number; }; 

Interface extends type alias

  1. type PartialPointX = { x: number; }; 
  2. interface Point extends PartialPointX { y: number; } 

Type alias extends interface

  1. interface PartialPointX { x: number; } 
  2. type Point = PartialPointX & { y: number; }; 

它們的差別可以看下面這圖或者看 TypeScript: Interfaces vs Types 。

所以檙想巧用 interface & type 還是不簡單的。

如果不知道用什么,記住:能用 interface 實現,就用 interface , 如果不能就用 type 。

4. typeof

typeof 操作符可以用來獲取一個變量或對象的類型。

我們一般先定義類型,再使用:

  1. interface Opt { 
  2.   timeout: number 
  3. const defaultOption: Opt = { 
  4.   timeout: 500 

有時候可以反過來:

  1. const defaultOption = { 
  2.   timeout: 500 
  3. type Opt = typeof defaultOption 

 

當一個 interface 總有一個字面量初始值時,可以考慮這種寫法以減少重復代碼,至少減少了兩行代碼是吧,哈哈~

5. keyof

TypeScript 允許我們遍歷某種類型的屬性,并通過 keyof 操作符提取其屬性的名稱。

keyof 操作符是在 TypeScript 2.1 版本引入的,該操作符可以用于獲取某種類型的所有鍵,其返回類型是聯合類型。

keyof 與 Object.keys 略有相似,只不過 keyof 取 interface 的鍵。

  1. const persion = { 
  2.   age: 3
  3.   text: 'hello world' 
  4.  
  5. // type keys = "age" | "text" 
  6. type keys = keyof Point; 

寫一個方法獲取對象里面的屬性值時,一般人可能會這么寫

  1. function get1(o: object, name: string) { 
  2.   return o[name]; 
  3.  
  4. const age1 = get1(persion, 'age'); 
  5. const text1 = get1(persion, 'text'); 

但是會提示報錯

因為 object 里面沒有事先聲明的 key。

當然如果把 o: object 修改為 o: any 就不會報錯了,但是獲取到的值就沒有類型了,也變成 any 了。

這時可以使用 keyof 來加強 get 函數的類型功能,有興趣的同學可以看看 _.get 的 type 標記以及實現

  1. function get<T extends object, K extends keyof T>(o: T, name: K): T[K] { 
  2.   return o[name] 

6. 查找類型

  1. interface Person { 
  2.     addr: { 
  3.         city: string, 
  4.         street: string, 
  5.         num: number, 
  6.     } 

當需要使用 addr 的類型時,除了把類型提出來

  1. interface Address { 
  2.     city: string, 
  3.     street: string, 
  4.     num: number, 
  5.  
  6. interface Person { 
  7.     addr: Address, 

還可以

  1. Person["addr"// This is Address. 

比如:

  1. const addr: Person["addr"] = { 
  2.     city: 'string'
  3.     street: 'string'
  4.     num: 2 

有些場合后者會讓代碼更整潔、易讀。

7. 查找類型 + 泛型 + keyof

泛型(Generics)是指在定義函數、接口或類的時候,不預先指定具體的類型,而在使用的時候再指定類型的一種特性。

  1. interface API { 
  2.     '/user': { name: string }, 
  3.     '/menu': { foods: string[] } 
  4. const get = <URL extends keyof API>(url: URL): Promise<API[URL]> => { 
  5.     return fetch(url).then(res => res.json()); 
  6.  
  7. get(''); 
  8. get('/menu').then(user => user.foods); 

 

8. 類型斷言

Vue 組件里面經常會用到 ref 來獲取子組件的屬性或者方法,但是往往都推斷不出來有啥屬性與方法,還會報錯。

子組件:

  1. <script lang="ts"
  2. import { Options, Vue } from "vue-class-component"
  3.  
  4. @Options({ 
  5.   props: { 
  6.     msg: String, 
  7.   }, 
  8. }) 
  9. export default class HelloWorld extends Vue { 
  10.   msg!: string; 
  11. </script> 

父組件:

  1. <template> 
  2.   <div class="home"
  3.     <HelloWorld 
  4.       ref="helloRef" 
  5.       msg="Welcome to Your Vue.js + TypeScript App" 
  6.     /> 
  7.   </div> 
  8. </template> 
  9.  
  10. <script lang="ts"
  11. import { Options, Vue } from "vue-class-component"
  12. import HelloWorld from "@/components/HelloWorld.vue"// @ is an alias to /src 
  13.  
  14. @Options({ 
  15.   components: { 
  16.     HelloWorld, 
  17.   }, 
  18. }) 
  19. export default class Home extends Vue { 
  20.   print() { 
  21.     const helloRef = this.$refs.helloRef; 
  22.     console.log("helloRef.msg: ", helloRef.msg);  
  23.   } 
  24.  
  25.   mounted() { 
  26.     this.print(); 
  27.   } 
  28. </script> 

因為 this.$refs.helloRef 是未知的類型,會報錯誤提示:

做個類型斷言即可:

  1. print() { 
  2.     // const helloRef = this.$refs.helloRef; 
  3.     const helloRef = this.$refs.helloRef as any; 
  4.     console.log("helloRef.msg: ", helloRef.msg); // helloRef.msg:  Welcome to Your Vue.js + TypeScript App 
  5.   } 

但是類型斷言為 any 時是不好的,如果知道具體的類型,寫具體的類型才好,不然引入 TypeScript 冒似沒什么意義了。

9. 顯式泛型

$('button') 是個 DOM 元素選擇器,可是返回值的類型是運行時才能確定的,除了返回 any ,還可以

  1. function $<T extends HTMLElement>(id: string): T { 
  2.     return (document.getElementById(id)) as T; 
  3.  
  4. // 不確定 input 的類型 
  5. // const input = $('input'); 
  6.  
  7. // Tell me what element it is. 
  8. const input = $<HTMLInputElement>('input'); 
  9. console.log('input.value: ', input.value); 

函數泛型不一定非得自動推導出類型,有時候顯式指定類型就好。

10. DeepReadonly

被 readonly 標記的屬性只能在聲明時或類的構造函數中賦值。

之后將不可改(即只讀屬性),否則會拋出 TS2540 錯誤。

與 ES6 中的 const 很相似,但 readonly 只能用在類(TS 里也可以是接口)中的屬性上,相當于一個只有 getter 沒有 setter 的屬性的語法糖。

下面實現一個深度聲明 readonly 的類型:

  1. type DeepReadonly<T> = { 
  2.   readonly [P in keyof T]: DeepReadonly<T[P]>; 
  3.  
  4. const a = { foo: { bar: 22 } } 
  5. const b = a as DeepReadonly<typeof a> 
  6. b.foo.bar = 33 // Cannot assign to 'bar' because it is a read-only property.ts(2540) 

 

責任編輯:張燕妮 來源: segmentfault.com
相關推薦

2020-05-22 18:19:34

微服務架構

2025-01-06 08:57:19

Vue技巧

2025-06-19 09:02:49

2017-11-30 19:23:24

2017-12-19 11:54:51

微信朋友圈同步

2012-07-12 13:20:35

職場生活程序員

2022-12-12 08:29:59

Vite構建工具

2011-04-06 12:22:28

2018-03-21 08:39:14

2017-03-29 13:12:56

Windows工具資訊

2019-04-12 08:19:15

快手AI算法

2022-12-07 14:40:39

能源

2012-01-11 14:58:08

2022-11-07 16:06:15

TypeScript開發技巧

2019-02-26 14:23:06

跳槽薪資職業

2017-10-30 15:22:29

代碼可讀性技巧

2019-09-26 08:33:51

Nginx技術Java

2019-01-15 11:40:14

開發技能代碼
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 2018天天干天天操 | 亚洲一区二区三区四区五区午夜 | 91久久精品国产91久久性色tv | 国产精品特级毛片一区二区三区 | av免费在线播放 | 亚洲国产成人精品久久 | 成人福利片 | 成年人免费看的视频 | 91热在线| 在线国产一区二区 | 午夜91| 国产成人在线看 | 爱综合 | 正在播放亚洲 | 91久久久久 | 成人免费久久 | 免费黄色片视频 | 欧美久久一级特黄毛片 | www.亚洲国产精品 | 亚洲综合无码一区二区 | 亚洲国产成人在线视频 | 中文字幕综合 | 国产免费一区二区 | 少妇淫片aaaaa毛片叫床爽 | 天天爱天天操 | av片免费观看| 国产一区二区三区免费观看在线 | 亚洲精品一区二三区不卡 | 亚洲成人国产综合 | 高清一区二区视频 | 中国一级特黄毛片大片 | 精品一二三区视频 | 欧美精品一区二区三区四区五区 | 中文字幕第九页 | 国产精品高潮呻吟 | 国产综合精品 | 午夜欧美 | 91精品国产综合久久久久久丝袜 | 久久国产精品免费一区二区三区 | 在线观看国产网站 | 久久久人成影片免费观看 |