# 🏅 Day 2 - 原始型別、型別註釋、型別推斷 ## 題目一:原始型別介紹 在 TypeScript 中,有幾種原始型別可以用來表示不同種類的資料。以下是幾個常見的原始型別: 1. 字串(string):用來表示文字型別。 2. 數字(number):用來表示數值的型別,可以是整數或浮點數。 3. 布林(boolean):用來表示真假值的型別,只有兩個值:`true` 和 `false`。 4. any:用來表示任意型別的型別,可以接受任何值。 ### 程式實做題 1. 請使用字串型別宣告一個名為 `name` 的變數,並將其設定為你的名字。 2. 請使用數字型別宣告一個名為 `age` 的變數,並將其設定為你的年齡。 3. 請使用布林型別宣告一個名為 `isStudent` 的變數,並將其設定為你是否是學生(是為 `true`,否為 `false`)。 4. 請使用 any 型別宣告一個名為 `data` 的變數,並將其設定為任意值。 # TypeScript **型別註釋與型別推斷** TypeScript 型別註釋與型別推斷是 TypeScript 中重要的概念。 **型別註釋**是在程式碼中明確指定變數或函式的型別。透過型別註釋,我們可以告訴編譯器變數的型別,以提供更好的靜態型別檢查。 **型別推斷**是 TypeScript 的一項功能,它可以自動推斷變數的型別,基於變數的初始化值和它在程式碼中的使用方式。當未明確指定型別時,型別推斷可以幫助我們省略冗長的型別註釋。 以下是兩個範例程式碼: **範例程式碼 1:使用型別註釋** ```tsx // 型別註釋 let name: string; let age: number; name = "John"; age = 25; console.log("Name:", name); console.log("Age:", age); ``` **範例程式碼 2:使用型別推斷** ```tsx // 型別推斷 let message = "Hello, TypeScript!"; let count = 10; console.log(message); console.log(count); ``` ### 單選題: 1. 型別註釋是強制一定要加上的嗎? - A. 是 - B. 否 2. 型別推斷基於什麼來推斷變數的型別? - A. 初始化值 - B. 函式參數 3. 型別推斷可以幫助我們省略哪些型別註釋? - A. 字串型別註釋 - B. 數字型別註釋 - C. 函式型別註釋 - D. 所有型別註釋 ### 開發題目: 1. 請使用型別註釋宣告一個變數 `score`,並將其設置為數字型別,初始值為 80。 2. 請使用型別推斷宣告一個變數 `message`,並將其設置為字串型別,初始值為 "Hello, TypeScript!"。 3. 請使用型別註釋宣告一個變數 `fullName`,並將其設置為字串型別,初始值為你的姓名。 ### 面試題(選做) 1. 你會如何判斷何時用型別推論或型別註釋? ## 回報流程 將答案寫在 CodePen,並貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看)  <!-- 解答: let name: string = "洧杰"; let age: number = 18; let isStudent: boolean = false; let data: any = "任意值"; 單選題目:B、A、D 開發題目: let score: number = 80; let message = "Hello, TypeScript!"; let fullName: string = "洧杰"; --> 回報區 --- | Discord | CodePen / 答案 | |:------------:|:-----------------------------------------------------------------------------------------------: | | 洧杰 | [Codepen](https://codepen.io/hexschool/pen/poYgYqW?editors=1010) | | kevinhes | [Codepen](https://codepen.io/kevinhes/pen/LYaZRrw?editors=0010) | | 展誠 | [Codepen](https://codepen.io/hedgehogkucc/pen/ExMygRr?editors=1010) | | HsienLu | [Codepen](https://codepen.io/Hsienlu/pen/wvOWzYe?editors=1111) | | 連小艾 | [Codepen](https://codepen.io/bolaslien/pen/PoLzGxW?editors=0010) | | 苡安 | [Codepen](https://codepen.io/yi-an-yang/pen/gOEMwym) | | hiYifang | [HackMD](https://hackmd.io/@gPeowpvtQX2Om6AmD-s3xw/B1VlA0_uT) | | YH.Lo | [Codepen](https://codepen.io/chloelo/pen/abMZBvo) | | YC | [HackMD](https://hackmd.io/SKoJd3EsTlitnjzCx4Rarg?view#🏅-Day-2---原始型別、型別註釋、型別推斷) | | 銀光菇 | [CodePen](https://codepen.io/genesynthesis/pen/jOJrMeZ) | | 77_0411 | [CodePen](https://codepen.io/chung-chi/pen/RwdRopg?editors=1010) | | BonnieChan | [CodePen](https://codepen.io/Bonnie-chan-the-bold/pen/abMZBWO?editors=0010) | | Yee | [CodePen](https://codepen.io/alivealife/pen/wvOWoYY) | | Derrick | [CodePen](https://codepen.io/Derricktang/pen/rNRLWXe) | | Jay | [CodePen](https://codepen.io/jayredk/pen/BabzQgG?editors=0010) | | Hannah | [CodePen](https://codepen.io/hannahpun/pen/mdoEWdN?editors=1010) | | Mi | [CodePen](https://codepen.io/Mi-Jou-Hsieh/pen/RwdRKEO?editors=1010) | | BEN | [CodePen](https://codepen.io/Kao-Yu-Chun/pen/rNRLmJN) | | Henry_Wu | [CodePen](https://codepen.io/hekman1122/pen/vYPKZdQ?editors=1010) | | Teddy | [CodePen](https://codepen.io/TaideLi/pen/XWGKgvV) | | Otis | [CodePen](https://codepen.io/humming74/pen/XWGKadq?editors=1011) | | jasperlu005 | [Codepen](https://codepen.io/uzzakuyr-the-reactor/pen/OJqXxdw?editors=1111) | | m_m | [CodePen](https://codepen.io/minnn7716/pen/vYPKWNj) | | Wendy_.li | [HACKMD](https://hackmd.io/PcmFgqZwRd-4Ep3-LgK5_Q) | | deedee1215 | [CodePen](https://codepen.io/diddy032/pen/KKEMZyX) | | Bryan Chu | [CodePen](https://codepen.io/bryanchu10/pen/XWGKVBy) | | hannahTW | [CodePen](https://codepen.io/hangineer/pen/MWxerXG?editors=1010) | | JC | [Codepen](https://codepen.io/jcsamoyed/pen/gOEMvpW?editors=0010) | | Zuo | [Codepen](https://codepen.io/linchinhsuan/pen/LYaZQxG?editors=1010) | | Shaokang | [Codepen](https://codepen.io/tony-hsueh/pen/OJqXQWO) | | leave3310 | [Codepen](https://codepen.io/leave3310-the-looper/pen/yLwJKey?editors=0010) | | rikku1756 | [Codepen](https://codepen.io/rikkubook/pen/ExMyEwX?editors=1010) | | NiuNiu | [Codepen](https://codepen.io/Dawson-the-bold/pen/MWxeVoy?editors=1010) | | LinaChen | [CodePen](https://codepen.io/LinaChen/pen/xxBOWQY) | | TrevorLin | [CodePen](https://codepen.io/0Trevor0/pen/wvOWpro) | | Mia | [CodePen](https://codepen.io/Mianzi/pen/JjzKvaq?editors=1010) | | Starr | [CodePen](https://codepen.io/StarrZhong/pen/rNRLvGK) | | zoe | [Codepen](https://codepen.io/Zoechiueh/pen/YzgWLaz?editors=0010) | | shan13 | [Codepen](https://codepen.io/yishan13-tsai/pen/WNmxJBd) | | ZS | [Codepen](https://codepen.io/irishuang/pen/eYXzKZX) | | ethan1331 | [Codepen](https://codepen.io/EthanTsai/pen/zYbBLxB) | | yunhung | [Codepen](https://codepen.io/ahung888/pen/WNmxyOK) | | Neil | [CodePen](https://codepen.io/Neil1024/pen/YzgWRoB?editors=0011) | | hiraka | [CodePen](https://codepen.io/judyyy/pen/ZEPOYEa) | | 亞當 | [CodePen](https://codepen.io/Adam-Hsu/pen/NWJroOO?editors=0010) | | Judy ☻ | [Codepen](https://codepen.io/hsiaohan/pen/XWGKwwm) | | Paul_09148 ☻ | [Codepen](https://codepen.io/JiaBaoShen/pen/wvOWLjL) | | wei | [Codepen](https://codepen.io/jweeei/pen/PoLzRbX) | | erwin阿瀚 | [Codepen](https://codepen.io/yohey03518/pen/ZEPpQqo) | | 時雨 | [Codepen](https://codepen.io/jjustin-35/pen/vYPXLvE?editors=1010) | | Yang🥸 | [Codepen](https://codepen.io/Yang-J/pen/rNRMxbq) | | chaya | [Codepen](https://codepen.io/vickyvvv9/pen/NWJRgjm) | | Jack | [codepen](https://codepen.io/lj787448952/pen/YzgGQVe) | | puffy | [codepen](https://codepen.io/TernMayDay/pen/qBvaVpX?editors=1010) | | nina.kuo | [codepen](https://codepen.io/ninakuo0814/pen/WNmGXPN) | | 精靈 | [CodePen](https://codepen.io/justafairy/pen/xxBEprJ) | | 薏慈 | [CodePen](https://codepen.io/its_wang/pen/bGZwLvv) | | Alyce | [CodePen](https://codepen.io/alycehwy/pen/mdorQdq) | | 楓 | [CodePen](https://codepen.io/maplestartend/pen/MWxbgYV) | | bonnieli1414 | [CodePen](https://codepen.io/bonnieli1414/pen/LYabNmV?editors=1011) | | Lisa | [CodePen](https://codepen.io/lisaha/pen/dyrOVJo?editors=1010) | | Tina | [CodePen](https://codepen.io/TinaTing/pen/PoLbQVK?editors=1010) | | 柴貓ouo | [CodePen](https://codepen.io/shibacat/pen/LYabmGQ) | | 皓皓 | [HackMD](https://hackmd.io/@cutecat8110/r1eRo4bKa) | | 翰毅 | [CodePen](https://codepen.io/yzuigtdw-the-animator/pen/mdoOZMq) | | Yao | [CodePen](https://codepen.io/AlbertoLL/pen/WNmoVbV) | |Amberhh |[codepen](https://codepen.io/Amberhh/pen/xxBgZKd?editors=1011) | | 黃士桓 | [codePen](https://codepen.io/shr-huan-huang/pen/BabpKOR) | | clairechang | [codePen](https://codepen.io/claire-chang-the-bashful/pen/JjzEXzP) | | 我是泇吟 | [codePen](https://codepen.io/kljuqbxs/pen/BabpRZL) | | Alex-Chiu | [codePen](https://codepen.io/datsxzqg-the-selector/pen/MWxedqd?editors=1010) | | Rochel | [Codepen](https://codepen.io/rochelwang1205/pen/MWxJqQj?editors=1010)| | MCBB_4881 | [Codepen](https://codepen.io/ebvaxaah-the-bashful/pen/dyrNLjK?editors=0010)| | 小米 | [Codepen](https://codepen.io/joanne-wei/pen/jOJBNMy?editors=0010)| |aki|[codepen](https://codepen.io/aki168/pen/GReWjpd)| |Louise|[codepen](https://codepen.io/LouiseHuang/pen/jOJBYaN)| | fanshu0303 | [CodePen](https://codepen.io/JuiHsuanLee0303/pen/LYaWgMv) | | Nick Lin | [CodePen](https://codepen.io/NickLinP/pen/dyrWPYv) | |Snorlax|[HackMD](https://hackmd.io/@snorlaxpock/SJY4eowYa)| |Yoshi|[CodePen](https://codepen.io/yoshiyyc/pen/ExMXYyX)| |Rena|[Codepen](https://codepen.io/aureate0116/pen/oNVwKzj) | | Han | [HACKMD](https://hackmd.io/@laihan/B18o2EaKa) | | Chelly | [Codepen](https://codepen.io/chellyhsu/pen/yLwzLJV) | | AmberL | [Codepen](https://codepen.io/autum55853/pen/ZEPajNy) | | 大衛 | [Codepen](https://codepen.io/exnsrpjc/pen/NWJXWYK?editors=1011) | | Eileen | [Codepen](https://codepen.io/Eileen-io/pen/wvOpVKw) | | 狸貓 | [Codepen](https://codepen.io/tanuki320/pen/jOJxYmg?editors=1011) | | 阿鬥鬥 | [Codepen](https://codepen.io/yvonne217/pen/oNVdRoJ) | | Tori | [HackMD](https://hackmd.io/OAdkiOH-S_WkD-LF6IONVA?view) |
×
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