From b1f25adeb59ea5f90feb3fa427104e07864a3f51 Mon Sep 17 00:00:00 2001 From: Worajedt Sitthidumrong Date: Tue, 13 Aug 2019 14:21:09 +0700 Subject: [PATCH 1/5] translate all paragraphs to Thai. --- th-th/typescript.th.html.markdown | 228 ++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 th-th/typescript.th.html.markdown diff --git a/th-th/typescript.th.html.markdown b/th-th/typescript.th.html.markdown new file mode 100644 index 00000000..94305c14 --- /dev/null +++ b/th-th/typescript.th.html.markdown @@ -0,0 +1,228 @@ +--- +language: TypeScript +contributors: + - ["Philippe Vlérick", "https://github.com/pvlerick"] + - ["Worajedt Sitthidumrong", "https://bitbucket.org/wrj"] +filename: learntypescript.ts +lang: th-th +--- + +TypeScript เป็นภาษาที่มีเป้าหมายเพื่อทำให้การพัฒนาซอฟต์แวร์ขนาดใหญ่ด้วย JavaScript ทำได้ง่ายขึ้น โดยที่ TypeScript ได้เพิ่มแนวคิดที่พบทั่วไป อาทิ classes, modules, interfaces, generics และ static typing (ไม่บังคับ) เข้าไปในภาษา JavaScript ดังนั้น TypeScript ก็เลยเป็น Super Set ของ JavaScript อีกที โค้ด JavaScript ทุกส่วน ก็คือโค้ดที่ทำงานได้ถูกต้องใน TypeScript ทำให้เราเพิ่ม TypeScript เข้าไปใช้ในโปรเจคการพัฒนาของเราได้ไม่ยากเลย เพราะ TypeScript คอมไพล์ผลลัพธ์ออกมาเป็น JavaScript ในท้ายสุดอยู่ดี + +บทความนี้จะเน้นเฉพาะ syntax ส่วนขยายของ TypeScript ซึ่งจะไม่รวมกับที่มีใน [JavaScript](/docs/javascript). + +การทดสอบเขียน TypeScript เริ่มได้ง่าย ๆ โดยเข้าไปที่ +[Playground] (http://www.typescriptlang.org/Playground) ซึ่งคุณจะเขียนโค้ดพร้อม autocomplete และเห็นเลยว่ามันจะแปลงมาเป็นผลลัพธ์แบบ JavaScript อย่างไร + +```ts +// There are 3 basic types in TypeScript +let isDone: boolean = false; +let lines: number = 42; +let name: string = "Anders"; + +// But you can omit the type annotation if the variables are derived +// from explicit literals +let isDone = false; +let lines = 42; +let name = "Anders"; + +// When it's impossible to know, there is the "Any" type +let notSure: any = 4; +notSure = "maybe a string instead"; +notSure = false; // okay, definitely a boolean + +// Use const keyword for constants +const numLivesForCat = 9; +numLivesForCat = 1; // Error + +// For collections, there are typed arrays and generic arrays +let list: number[] = [1, 2, 3]; +// Alternatively, using the generic array type +let list: Array = [1, 2, 3]; + +// For enumerations: +enum Color { Red, Green, Blue }; +let c: Color = Color.Green; + +// Lastly, "void" is used in the special case of a function returning nothing +function bigHorribleAlert(): void { + alert("I'm a little annoying box!"); +} + +// Functions are first class citizens, support the lambda "fat arrow" syntax and +// use type inference + +// The following are equivalent, the same signature will be inferred by the +// compiler, and same JavaScript will be emitted +let f1 = function (i: number): number { return i * i; } +// Return type inferred +let f2 = function (i: number) { return i * i; } +// "Fat arrow" syntax +let f3 = (i: number): number => { return i * i; } +// "Fat arrow" syntax with return type inferred +let f4 = (i: number) => { return i * i; } +// "Fat arrow" syntax with return type inferred, braceless means no return +// keyword needed +let f5 = (i: number) => i * i; + +// Interfaces are structural, anything that has the properties is compliant with +// the interface +interface Person { + name: string; + // Optional properties, marked with a "?" + age?: number; + // And of course functions + move(): void; +} + +// Object that implements the "Person" interface +// Can be treated as a Person since it has the name and move properties +let p: Person = { name: "Bobby", move: () => { } }; +// Objects that have the optional property: +let validPerson: Person = { name: "Bobby", age: 42, move: () => { } }; +// Is not a person because age is not a number +let invalidPerson: Person = { name: "Bobby", age: true }; + +// Interfaces can also describe a function type +interface SearchFunc { + (source: string, subString: string): boolean; +} +// Only the parameters' types are important, names are not important. +let mySearch: SearchFunc; +mySearch = function (src: string, sub: string) { + return src.search(sub) != -1; +} + +// Classes - members are public by default +class Point { + // Properties + x: number; + + // Constructor - the public/private keywords in this context will generate + // the boiler plate code for the property and the initialization in the + // constructor. + // In this example, "y" will be defined just like "x" is, but with less code + // Default values are also supported + + constructor(x: number, public y: number = 0) { + this.x = x; + } + + // Functions + dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } + + // Static members + static origin = new Point(0, 0); +} + +// Classes can be explicitly marked as implementing an interface. +// Any missing properties will then cause an error at compile-time. +class PointPerson implements Person { + name: string + move() {} +} + +let p1 = new Point(10, 20); +let p2 = new Point(25); //y will be 0 + +// Inheritance +class Point3D extends Point { + constructor(x: number, y: number, public z: number = 0) { + super(x, y); // Explicit call to the super class constructor is mandatory + } + + // Overwrite + dist() { + let d = super.dist(); + return Math.sqrt(d * d + this.z * this.z); + } +} + +// Modules, "." can be used as separator for sub modules +module Geometry { + export class Square { + constructor(public sideLength: number = 0) { + } + area() { + return Math.pow(this.sideLength, 2); + } + } +} + +let s1 = new Geometry.Square(5); + +// Local alias for referencing a module +import G = Geometry; + +let s2 = new G.Square(10); + +// Generics +// Classes +class Tuple { + constructor(public item1: T1, public item2: T2) { + } +} + +// Interfaces +interface Pair { + item1: T; + item2: T; +} + +// And functions +let pairToTuple = function (p: Pair) { + return new Tuple(p.item1, p.item2); +}; + +let tuple = pairToTuple({ item1: "hello", item2: "world" }); + +// Including references to a definition file: +/// + +// Template Strings (strings that use backticks) +// String Interpolation with Template Strings +let name = 'Tyrone'; +let greeting = `Hi ${name}, how are you?` +// Multiline Strings with Template Strings +let multiline = `This is an example +of a multiline string`; + +// READONLY: New Feature in TypeScript 3.1 +interface Person { + readonly name: string; + readonly age: number; +} + +var p1: Person = { name: "Tyrone", age: 42 }; +p1.age = 25; // Error, p1.x is read-only + +var p2 = { name: "John", age: 60 }; +var p3: Person = p2; // Ok, read-only alias for p2 +p3.x = 35; // Error, p3.x is read-only +p2.x = 45; // Ok, but also changes p3.x because of aliasing + +class Car { + readonly make: string; + readonly model: string; + readonly year = 2018; + + constructor() { + this.make = "Unknown Make"; // Assignment permitted in constructor + this.model = "Unknown Model"; // Assignment permitted in constructor + } +} + +let numbers: Array = [0, 1, 2, 3, 4]; +let moreNumbers: ReadonlyArray = numbers; +moreNumbers[5] = 5; // Error, elements are read-only +moreNumbers.push(5); // Error, no push method (because it mutates array) +moreNumbers.length = 3; // Error, length is read-only +numbers = moreNumbers; // Error, mutating methods are missing +``` + +## อ่านเพิ่มเติมที่ + * [TypeScript Official website] (http://www.typescriptlang.org/) + * [TypeScript language specifications] (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md) + * [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript) + * [Source Code on GitHub] (https://github.com/Microsoft/TypeScript) + * [Definitely Typed - repository for type definitions] (http://definitelytyped.org/) From e616d439632bf17e76685de8298247ee58b01067 Mon Sep 17 00:00:00 2001 From: Worajedt Sitthidumrong Date: Tue, 13 Aug 2019 14:44:19 +0700 Subject: [PATCH 2/5] translate to Thai up to interface topic --- th-th/typescript.th.html.markdown | 42 ++++++++++++++----------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/th-th/typescript.th.html.markdown b/th-th/typescript.th.html.markdown index 94305c14..ce1d3bb8 100644 --- a/th-th/typescript.th.html.markdown +++ b/th-th/typescript.th.html.markdown @@ -15,63 +15,59 @@ TypeScript เป็นภาษาที่มีเป้าหมายเพ [Playground] (http://www.typescriptlang.org/Playground) ซึ่งคุณจะเขียนโค้ดพร้อม autocomplete และเห็นเลยว่ามันจะแปลงมาเป็นผลลัพธ์แบบ JavaScript อย่างไร ```ts -// There are 3 basic types in TypeScript +// TypeScript มี data type พื้นฐาน 3 แบบ let isDone: boolean = false; let lines: number = 42; let name: string = "Anders"; -// But you can omit the type annotation if the variables are derived -// from explicit literals +// แต่เราก็สามารถละการบอกชนิดได้ โดยชนิดตัวแปรก็จะปรับชนิดของเขาจากข้อมูลที่กำหนดให้โดยตรง let isDone = false; let lines = 42; let name = "Anders"; -// When it's impossible to know, there is the "Any" type +// ถ้าไม่รู้ ก็กำหนดเป็นชนิด "Any" ได้ let notSure: any = 4; notSure = "maybe a string instead"; -notSure = false; // okay, definitely a boolean +notSure = false; // โอเค ตอนนี้เป็น Boolean แน่ ๆ -// Use const keyword for constants +// ใช้ const สำหรับสร้าง ค่าคงที่ const numLivesForCat = 9; numLivesForCat = 1; // Error -// For collections, there are typed arrays and generic arrays +// สำหรับ collections มี typed arrays และ generic arrays +// ก็คือ อะเรย์บอกชนิด และ อะเรย์เจเนอริก ตามลำดับ let list: number[] = [1, 2, 3]; -// Alternatively, using the generic array type +// ในอีกทางหนึ่ง สร้างเป็นอะเรย์ชนิด generic array let list: Array = [1, 2, 3]; -// For enumerations: +// และสำหรับ enumerations: enum Color { Red, Green, Blue }; let c: Color = Color.Green; -// Lastly, "void" is used in the special case of a function returning nothing +// สุดท้าย, "void" ใช้เมื่อเป็นกรณีพิเศษที่ฟังก์ชันไม่ส่งค่ากลับ function bigHorribleAlert(): void { alert("I'm a little annoying box!"); } -// Functions are first class citizens, support the lambda "fat arrow" syntax and -// use type inference +// ฟังก์ชั่น (Functions) เป็นสิ่งที่มีความสำคัญมาเป็นอันดับหนึ่ง รองรับการใช้ "fat arrow" ในการสร้าง lambda function และ type inference -// The following are equivalent, the same signature will be inferred by the -// compiler, and same JavaScript will be emitted +// สไตล์ต่อไปนี้มีค่าเท่ากันกับบรรทัดที่ยกตัวอย่างด้านล่าง เพราะคอมไพเลอร์จะมองเหมือนกัน และได้ JavaScript แบบเดียวกัน let f1 = function (i: number): number { return i * i; } -// Return type inferred +// อนุมานชนิดที่ส่งกลับ หรือ type inferred let f2 = function (i: number) { return i * i; } -// "Fat arrow" syntax +// เขียนแบบ "Fat arrow" แต่บอกชนิดส่งกลับ let f3 = (i: number): number => { return i * i; } -// "Fat arrow" syntax with return type inferred +// เขียนแบบ "Fat arrow" แต่อนุมานชนิดส่งกลับ let f4 = (i: number) => { return i * i; } -// "Fat arrow" syntax with return type inferred, braceless means no return -// keyword needed +// เขียนแบบ "Fat arrow" อนุมานชนิดส่งกลับ พร้อมกับไม่มีวงเล็บ แปลว่าไม่ต้องมี return keyword ด้วย let f5 = (i: number) => i * i; -// Interfaces are structural, anything that has the properties is compliant with -// the interface +// Interfaces นั้นเป็นเหมือนเราออกแบบโครงสร้าง คุณสมบัติต่าง ๆ ตอนเอาไปใช้ จะต้องเป็นไปตาม interface นั้น ๆ interface Person { name: string; - // Optional properties, marked with a "?" + // Optional properties กำหนดด้วย "?" age?: number; - // And of course functions + // และมี function พร้อมชนิดได้ใน interface move(): void; } From 121627522d97be99958b9c18e77a47e77cf12a76 Mon Sep 17 00:00:00 2001 From: Worajedt Sitthidumrong Date: Tue, 13 Aug 2019 16:02:01 +0700 Subject: [PATCH 3/5] finish translate TypeScript to Thai language --- th-th/typescript.th.html.markdown | 84 +++++++++++++++---------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/th-th/typescript.th.html.markdown b/th-th/typescript.th.html.markdown index ce1d3bb8..29f9b16e 100644 --- a/th-th/typescript.th.html.markdown +++ b/th-th/typescript.th.html.markdown @@ -9,10 +9,10 @@ lang: th-th TypeScript เป็นภาษาที่มีเป้าหมายเพื่อทำให้การพัฒนาซอฟต์แวร์ขนาดใหญ่ด้วย JavaScript ทำได้ง่ายขึ้น โดยที่ TypeScript ได้เพิ่มแนวคิดที่พบทั่วไป อาทิ classes, modules, interfaces, generics และ static typing (ไม่บังคับ) เข้าไปในภาษา JavaScript ดังนั้น TypeScript ก็เลยเป็น Super Set ของ JavaScript อีกที โค้ด JavaScript ทุกส่วน ก็คือโค้ดที่ทำงานได้ถูกต้องใน TypeScript ทำให้เราเพิ่ม TypeScript เข้าไปใช้ในโปรเจคการพัฒนาของเราได้ไม่ยากเลย เพราะ TypeScript คอมไพล์ผลลัพธ์ออกมาเป็น JavaScript ในท้ายสุดอยู่ดี -บทความนี้จะเน้นเฉพาะ syntax ส่วนขยายของ TypeScript ซึ่งจะไม่รวมกับที่มีใน [JavaScript](/docs/javascript). +บทความนี้จะเน้นเฉพาะ syntax ส่วนขยายของ TypeScript ซึ่งจะไม่รวมกับที่มีใน [JavaScript](/docs/javascript) การทดสอบเขียน TypeScript เริ่มได้ง่าย ๆ โดยเข้าไปที่ -[Playground] (http://www.typescriptlang.org/Playground) ซึ่งคุณจะเขียนโค้ดพร้อม autocomplete และเห็นเลยว่ามันจะแปลงมาเป็นผลลัพธ์แบบ JavaScript อย่างไร +[Playground](http://www.typescriptlang.org/Playground) ซึ่งคุณจะเขียนโค้ดพร้อม autocomplete และเห็นเลยว่ามันจะแปลงมาเป็นผลลัพธ์แบบ JavaScript อย่างไร ```ts // TypeScript มี data type พื้นฐาน 3 แบบ @@ -62,7 +62,7 @@ let f4 = (i: number) => { return i * i; } // เขียนแบบ "Fat arrow" อนุมานชนิดส่งกลับ พร้อมกับไม่มีวงเล็บ แปลว่าไม่ต้องมี return keyword ด้วย let f5 = (i: number) => i * i; -// Interfaces นั้นเป็นเหมือนเราออกแบบโครงสร้าง คุณสมบัติต่าง ๆ ตอนเอาไปใช้ จะต้องเป็นไปตาม interface นั้น ๆ +// Interfaces นั้นเป็นเหมือนเราออกแบบโครงสร้าง คุณสมบัติต่าง ๆ ตอนเอาไปใช้ จะต้องเป็นไปตาม interface นั้น ๆ เหมือนกับเป็นการกำหนดสเป็คของ "ชนิดข้อมูล" interface Person { name: string; // Optional properties กำหนดด้วย "?" @@ -71,70 +71,70 @@ interface Person { move(): void; } -// Object that implements the "Person" interface -// Can be treated as a Person since it has the name and move properties +// Object นี้ implements "Person" interface ทำให้มันเป็นชนิด Person และมันก็มี property name และ function move() let p: Person = { name: "Bobby", move: () => { } }; -// Objects that have the optional property: +// Objects นี้เป็นชนิด Person ด้วย และมี optional property หรือ age?: นั่นเอง let validPerson: Person = { name: "Bobby", age: 42, move: () => { } }; -// Is not a person because age is not a number +// ไม่ใช่ Person เพราะ age: ต้องเป็น number เท่านั้น ตามข้อกำหนดใน interface Person let invalidPerson: Person = { name: "Bobby", age: true }; -// Interfaces can also describe a function type +// Interfaces ยังนำมาใช้ในลักษณะของ function ได้อีกด้วย interface SearchFunc { (source: string, subString: string): boolean; } -// Only the parameters' types are important, names are not important. +// เฉพาะชนิด parameters เท่านั้นที่สำคัญ ชื่อของมันไม่จำเป็นต้องเหมือน let mySearch: SearchFunc; mySearch = function (src: string, sub: string) { return src.search(sub) != -1; } -// Classes - members are public by default +// สมาชิกใน class จะเข้าถึงได้แบบ public เป็นค่าปริยาย class Point { // Properties + // ตั้งค่า Properties ของ class นี้ x: number; - // Constructor - the public/private keywords in this context will generate - // the boiler plate code for the property and the initialization in the - // constructor. - // In this example, "y" will be defined just like "x" is, but with less code - // Default values are also supported + // Constructor + // เราใส่ public/private keywords ตรงนี้ได้ มีผลเหมือนกันกับกำหนด x ด้านบน + // ในตัวอย่าง y มีการกำหนดเช่นเดียวกับ x แต่พิมพ์สั้นกว่า + // สังเกตว่า มีการกำหนดค่าปริยายให้ parameters ได้ด้วย constructor(x: number, public y: number = 0) { this.x = x; } // Functions - dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } + dist() { return Math.sqrt(this.x*this.x + this.y*this.y); } // Static members static origin = new Point(0, 0); } -// Classes can be explicitly marked as implementing an interface. -// Any missing properties will then cause an error at compile-time. +// Classes สามารถระบุชนิด interface ที่ต้องการได้ตรง ๆ ด้วยเช่นโค้ดด้านล่าง +// แต่อะไรที่จะ implement มานั้น ถ้าไม่ได้กำหนดไว้ใน constructor ก็จะเกิดข้อผิดพลาดตอนคอมไพล์ class PointPerson implements Person { - name: string + name: string // ตรงนี้จะผิด แก้ไขโดยการไปสร้างตัวรับค่าเข้ามาผ่านทาง constructor move() {} } let p1 = new Point(10, 20); -let p2 = new Point(25); //y will be 0 +let p2 = new Point(25); //y เป็น 0 เพราะกำหนดค่าปริยายไว้ให้แล้ว -// Inheritance +// Inheritance การสืบทอดคุณสมบัติ class Point3D extends Point { constructor(x: number, y: number, public z: number = 0) { - super(x, y); // Explicit call to the super class constructor is mandatory + super(x, y); // เราจะต้องเรียกใช้ super class constructor โดยตรง } - // Overwrite + // Overwrite ฟังก์ชั่นที่มีอยู่เดิมใน Point dist() { let d = super.dist(); return Math.sqrt(d * d + this.z * this.z); } } -// Modules, "." can be used as separator for sub modules +// Modules ใช้เป็นกลุ่มของ class เราใช้ "." เป็นตัวแบ่ง sub modules +// อย่างกรณีนี้จะเป็น Module.Class เช่น Geometry.Square module Geometry { export class Square { constructor(public sideLength: number = 0) { @@ -147,7 +147,7 @@ module Geometry { let s1 = new Geometry.Square(5); -// Local alias for referencing a module +// เราทำให้เรียกใช้ง่ายขึ้นโดยการใช้ alias มาอ้างชื่อ module แบบเดียวกับบางภาษา เช่น Python import G = Geometry; let s2 = new G.Square(10); @@ -165,37 +165,37 @@ interface Pair { item2: T; } -// And functions +// และ functions let pairToTuple = function (p: Pair) { return new Tuple(p.item1, p.item2); }; let tuple = pairToTuple({ item1: "hello", item2: "world" }); -// Including references to a definition file: +// เราเรียกใช้ไฟล์ definition แบบนี้: /// -// Template Strings (strings that use backticks) -// String Interpolation with Template Strings +// Template Strings ( คือ strings ที่ใช้ backticks ครอบ — "`" ปุ่มบนซ้ายคีย์บอร์ด ) +// แทรกข้อความใน String ด้วย Template Strings let name = 'Tyrone'; let greeting = `Hi ${name}, how are you?` -// Multiline Strings with Template Strings +// Strings หลายบรรทัดก็ทำได้ใน Template Strings let multiline = `This is an example of a multiline string`; -// READONLY: New Feature in TypeScript 3.1 +// READONLY: ความสามารถใหม่ใน TypeScript 3.1 interface Person { readonly name: string; readonly age: number; } var p1: Person = { name: "Tyrone", age: 42 }; -p1.age = 25; // Error, p1.x is read-only +p1.age = 25; // Error แน่นอน เพราะ p1.x ถูกกำหนดเป็น read-only -var p2 = { name: "John", age: 60 }; -var p3: Person = p2; // Ok, read-only alias for p2 -p3.x = 35; // Error, p3.x is read-only -p2.x = 45; // Ok, but also changes p3.x because of aliasing +var p2 = { name: "John", age: 60 }; // สังเกตว่า p2 ไม่ได้กำหนดเป็น Person +var p3: Person = p2; // ทำได้ เป็น read-only alias ของ p2 และกำหนดเป็น Person +p3.x = 35; // Error p3.x ก็เป็น read-only +p2.x = 45; // Ok ทำได้แต่ก็จะเปลี่ยนค่า p3.x ด้วย เพราะ p3 เป็น alias ของ p2 class Car { readonly make: string; @@ -203,17 +203,17 @@ class Car { readonly year = 2018; constructor() { - this.make = "Unknown Make"; // Assignment permitted in constructor - this.model = "Unknown Model"; // Assignment permitted in constructor + this.make = "Unknown Make"; // อนุญาตให้กำหนดค่าได้ใน constructor แม้ว่าจะ read-only + this.model = "Unknown Model"; // อนุญาตให้กำหนดค่าได้ใน constructor แม้ว่าจะ read-only } } let numbers: Array = [0, 1, 2, 3, 4]; let moreNumbers: ReadonlyArray = numbers; -moreNumbers[5] = 5; // Error, elements are read-only -moreNumbers.push(5); // Error, no push method (because it mutates array) -moreNumbers.length = 3; // Error, length is read-only -numbers = moreNumbers; // Error, mutating methods are missing +moreNumbers[5] = 5; // Error, สมาชิกอะเรย์เป็น read-only แปลว่า ห้ามแก้ไข +moreNumbers.push(5); // Error, push method ใช้ไม่ได้ เพราะมันจะไปแก้ไข read-only array +moreNumbers.length = 3; // Error, เพราะ length ก็ต้อง read-only +numbers = moreNumbers; // Error, method ที่ทำให้อะเรย์เปลี่ยนได้จะไม่อนุญาต ``` ## อ่านเพิ่มเติมที่ From 7e06e77a44cba2695157ba1edaeb7c24561d0def Mon Sep 17 00:00:00 2001 From: Worajedt Sitthidumrong Date: Tue, 13 Aug 2019 16:22:32 +0700 Subject: [PATCH 4/5] additional translation for Iterators, Generators --- th-th/typescript.th.html.markdown | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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 +} + + ``` ## อ่านเพิ่มเติมที่ From 65ab14ca41d58670e73874ea31c8574637d8e749 Mon Sep 17 00:00:00 2001 From: Worajedt Sitthidumrong Date: Wed, 14 Aug 2019 02:34:53 +0700 Subject: [PATCH 5/5] fix file name per request 'learntypescript-th.ts' --- th-th/typescript.th.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/th-th/typescript.th.html.markdown b/th-th/typescript.th.html.markdown index 88b0fc0b..fc2a823b 100644 --- a/th-th/typescript.th.html.markdown +++ b/th-th/typescript.th.html.markdown @@ -3,7 +3,7 @@ language: TypeScript contributors: - ["Philippe Vlérick", "https://github.com/pvlerick"] - ["Worajedt Sitthidumrong", "https://bitbucket.org/wrj"] -filename: learntypescript.ts +filename: learntypescript-th.ts lang: th-th ---