--- GA: G-7BSJTG7RYN --- 我們在上一篇 [泛用化名](https://hackmd.io/@elzuoc/BJie-0Le3#%E6%B3%9B%E7%94%A8%E5%8C%96%E5%90%8D-Generic-Type-Alias) 有看到一個範例: ```typescript= type Dictionary<T> = { [prop: string]: T }; ``` 其中 `[prop: string]` 就是索引型別的使用方式。 我們先用熟悉的 型別化名 來瞭解: ```typescript= type MyObjectType = { [key: string]: number }; ``` 這表示我們接下來註記為 `MyObjectType` 的變數,其 `key-value` 的型別有一定格式: > 屬性(或稱為 `key`)型別必須是 `string`; > 屬性值(或稱為 `value`)型別必須是 `number`。 ```typescript= const obj: MyObjectType = { width: 45, height: 90, }; ``` --- ### Reference - [讓TypeScript成為你全端開發的ACE!- Maxwell Huang](https://www.books.com.tw/products/0010859134) - [讓 TypeScript 成為你全端開發的 ACE! 系列 - 第 11 屆 iThome 鐵人賽](https://ithelp.ithome.com.tw/users/20120614/ironman/2685) - [TypeScript Official Site](https://www.typescriptlang.org/docs) --- > 系列:[`跑完 JS30 就接著認識 TypeScript 入門`](https://hackmd.io/@elzuoc?tags=%5B%22%E8%B7%91%E5%AE%8C+JS30+%E5%B0%B1%E6%8E%A5%E8%91%97%E8%AA%8D%E8%AD%98+TypeScript+%E5%85%A5%E9%96%80%22%5D) > 上一篇:[Day07:型別系統 - 泛用型別 Generic Types](https://hackmd.io/@elzuoc/BJie-0Le3) > 下一篇:[Day09:認識介面(Interface)](https://hackmd.io/@elzuoc/ryw1Am_xn) ###### tags: [`跑完 JS30 就接著認識 TypeScript 入門`](https://hackmd.io/@elzuoc?tags=%5B%22%E8%B7%91%E5%AE%8C+JS30+%E5%B0%B1%E6%8E%A5%E8%91%97%E8%AA%8D%E8%AD%98+TypeScript+%E5%85%A5%E9%96%80%22%5D) ###### Created on ∥ March 22, 2023 ###### 文章若有任何錯誤,還請不吝給予留言指正,謝謝大家!
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up