DEV Community

Cover image for รีวิวคอร์ส Introduction Typescript‍
Thanawat Gulati
Thanawat Gulati

Posted on • Originally published at Medium on

รีวิวคอร์ส Introduction Typescript‍

ต้องขอท้าวความก่อนว่าทำไมถึงมาเริ่มสนใจการใช้ Typescript ก็เพราะว่าได้เริ่มเรียนรู้และ เริ่มสนใจจากการได้เรียน กับ คอร์สสดของ The Self Made หากใครสนใจลองไปดูได้ที่ คลิกที่นี่

แล้วได้ทดลองเปลี่ยนจากการเขียน JavaScript มาลงเป็น Typescript แล้วรู้สึกว่า Typescript ก็มีความเขียนสนุก วันนี้จะมาลองเขียน Typescript ตาม docs และที่พี่เขา ให้ลองดูๆเพื่อใครสนใจจะศึกษาเพิ่มเติม

ก่อนอื่นต้องบอกก่อนว่า Typescript การเขียนจะต่างจาก Javascript เล็กน้อยตรงที่ Typescript จะต้องระบุ Type ให้กับ ไม่ว่าจะเป็น Variable หรือ Method อื่นๆ

ตัวอย่าง Data-Type

Boolean / String / Number / Any

const isLoadingTS: boolean = true; // Boolean แบบ Typescript
const cat: string = "CAT"; // String แบบ Typescript
const quantity: number = 300; // Number แบบ Typescript

// Number จะสามาระใช้ได้ แบบทั้ง Decimal , Hex , Octal , Binary
const deciamal:numebr = 100; 
const hex: number = 0xf66a0; // 0 - 9 & a - f
const binary: number = 0b00101; // 0 , 1
const octal: number = 0o31752; // 0 - 7
const anyType : any = "james" // สามาถใช้ Type อะไรก็ได้
Enter fullscreen mode Exit fullscreen mode

ตัวอย่าง Method

Void / Number / String / Boolean / Any

// การ Return แบบ Void
const voidReturn = () : **void** => {
    console.log("Hello World")
}

// การ Return แบบ Number
const numberReturn = () : **number** => {
    const **num** : number = 10;
    return **num** ;
}

// การ Return แบบ String
const stringReturn = () : **string** => {
    const **name** = "James";
    return **name** ;
}

// การ Return แบบ Boolean
const boolReturn = () : **boolean** => {
    const **isLogin** = true;
    return **isLogin** ;
}

// การ Return แบบ Any
const boolReturn = () : **any** => {
    const **word** = "Hello Any";
    return **word** ;
}

// การ Return แบบ Never สำหรับ method ที่ทำงานไม่สิ้นสุด
const boolReturn = () : **never** => {
    **while(true) {  
 console.log("Hello From the outside");  
 }**  
}

// และถ้าหากมีการรับ parameter ก็จะต้องใส่. Type ให้กับ parameter ด้วย
const stringReturn/ = ( **name : string** ) : **string** => {
    return **name** ;
}
Enter fullscreen mode Exit fullscreen mode

จะบอกว่าคอร์สนี้เป็นคอร์สที่ดีที่ได้ทั้งทำบุญ ได้ความรู้ และได้แรงบันดาลใจในการเขียน Typescript เพราะตอนแรกคือเคยลองเขียนเองแล้ว งง นิดหน่อย แต่นานมากละ หลังจากได้เรียนก็เปิดรับมันมากขึ้นและสนุกไปกับมัน

ยังมีเรื่องอื่นๆอีกมากมายที่น่าสนใจไม่ว่าจะ Enum , interface , Gerneric Type ที่สามารถไปศึกษาเพิ่มเติมได้ที่

antronic/theselfmade-typescript

ติดตามเราได้ที่

- thanawatgulati - Overview


Latest comments (0)