From 7e06e77a44cba2695157ba1edaeb7c24561d0def Mon Sep 17 00:00:00 2001 From: Worajedt Sitthidumrong Date: Tue, 13 Aug 2019 16:22:32 +0700 Subject: additional translation for Iterators, Generators --- th-th/typescript.th.html.markdown | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'th-th') diff --git a/th-th/typescript.th.html.markdown b/th-th/typescript.th.html.markdown index 29f9b16e..88b0fc0b 100644 --- a/th-th/typescript.th.html.markdown +++ b/th-th/typescript.th.html.markdown @@ -214,6 +214,41 @@ moreNumbers[5] = 5; // Error, สมาชิกอะเรย์เป็ moreNumbers.push(5); // Error, push method ใช้ไม่ได้ เพราะมันจะไปแก้ไข read-only array moreNumbers.length = 3; // Error, เพราะ length ก็ต้อง read-only numbers = moreNumbers; // Error, method ที่ทำให้อะเรย์เปลี่ยนได้จะไม่อนุญาต + +// Tagged Union Types สำหรับโมเดลสเตท ที่อาจจะมีได้หลายๆ สเตท +type State = + | { type: "loading" } + | { type: "success", value: number } + | { type: "error", message: string }; + +ประกาศ const state: State; +if (state.type === "success") { + console.log(state.value); +} else if (state.type === "error") { + console.error(state.message); +} + +// Iterators และ Generators + +// ประโยคแบบ for..of +// การทำซ้ำกับ ลิสต์ของค่าในออปเจ็คต์ +let arrayOfAnyType = [1, "string", false]; +for (const val of arrayOfAnyType) { + console.log(val); // 1, "string", false +} + +let list = [4, 5, 6]; +for (const i of list) { + console.log(i); // 4, 5, 6 +} + +// ประโยคแบบ for..in +// การทำซ้ำกับ ลิสต์ของคีย์ในออปเจ็คต์ +for (const i in list) { + console.log(i); // 0, 1, 2 +} + + ``` ## อ่านเพิ่มเติมที่ -- cgit v1.2.3