let's get IT with DAVINA ๐Ÿ’ป

TypeScript๋ž€? & Types ๋ณธ๋ฌธ

DEV_IN/TypeScript

TypeScript๋ž€? & Types

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 9. 04:46

TypeScript

  • Javascript with syntax for types
  • a strongly typed programming language that builds on JavaScript, giving you better tool at any scale
  • JS์˜ ํƒ€์ž… ์•ˆ์ •์„ฑ โฌ† ๋ฒ„์ „!
  • TypeScript & VS Code ⇒ ๋‘˜ ๋‹ค Microsoft์—์„œ ๊ฐœ๋ฐœํ•ด์„œ ์ฃฝ์ด ์ž˜ ๋งž์•„

JavaScript VS TypeScript

  • JS ⇒ ๊ฐœ๋ฐœ์ž๋“ค์ด ๋ฉ์ฒญํ•œ ์‹ค์ˆ˜๋ฅผ ํ•ด๋„ ์ดํ•ดํ•ด์ฃผ๋ ค ํ•˜๋Š” ์œ ์—ฐํ•œ ์–ธ์–ด
    • [1,2,3,4]+ false(boolean) = “1,2,3,4false”(string)
    • ⇒ TS๋Š” ๋ง๋„ ์•ˆ๋˜๋Š” ํ˜ธ์ถœ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ
    • ๋ฌธ์ œ์žˆ๋Š” ํ˜ธ์ถœ์„ ์œ ์ €์˜ ์ปดํ“จํ„ฐ์—์„œ ์ด๋ฏธ ์‹คํ–‰ํ•˜๊ณ  ๋‚˜์„œ ์—๋Ÿฌ๋ผ๊ณ  ๋งํ•ด์คŒ
      • ์ด๊ฒƒ์ด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ!! = ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์•Œ ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ..์œ„ํ—˜ํ•ด
    • ⇒ TS๋Š” (์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์—) ํ˜ธ์ถœ์„ ํ•˜๊ธฐ๋„ ์ „์— ๋ง‰์•„์คŒ
    const nico = {name:"nico"}
    
    nico.hello() => uncaught TypeError: nico.hello is not a function

Overview of Typescript

2-1. TS ์ž‘๋™์ด ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฑธ๊นŒ?

  1. Typescript๋ฅผ ์ž‘์„ฑํ•œ๋‹ค
  2. Compile์„ ํ†ตํ•ด TS→JS๋กœ ๋ณ€ํ™˜ (๋ธŒ๋ผ์šฐ์ €๋Š” JS์ฝ”๋“œ๋งŒ ์ฝ์„ ์ˆ˜ ์žˆ์–ด์„œ ๋ณ€ํ™˜ํ•˜๋Š”๊ฑฐ์ž„)
  3. TS ์ฝ”๋“œ์— ์—๋Ÿฌ๊ฐ€ ์žˆ์œผ๋ฉด ์• ์ดˆ์— JS๋กœ ์ปดํŒŒ์ผ๋˜์ง€ ์•Š์Œ

2-2. Explicit VS Implicit Types

  • Explicit Types: Type Checker (TS์˜ syntax)
    • ํƒ€์ž…์ด ๋ญ”์ง€ ๋ช…ํ™•ํžˆ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ!
    let a : string = "hi"
    let b : boolean = false
    let c : number[] //์ˆซ์ž๋กœ ์ด๋ค„์ง„ ๋ฐฐ์—ด์ด์•ผ! ์ง€์ •!
  • Implicit Types
    • ํƒ€์ž…์„ TS๊ฐ€ ์•Œ์•„์„œ ์ถ”๋ก ํ•˜๋Š”๊ฒƒ
    let a = "hello" //string์ด๊ตฌ๋‚˜~

2-3. Types of TS (1)

  • age? ์ฒ˜๋Ÿผ ๊ฐ’์˜ ์œ ๋ฌด๋ฅผ optional๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ undefined ๋ฐ˜ํ™˜!
const player : { //player์€ ๊ฐ์ฒด์•ผ
    name:string, //name์€ stringํƒ€์ž…์ด์•ผ
    age?:number //age๋Š” optionalํ•ด์„œ ์žˆ์–ด๋„ ๋˜๊ณ , ์—†์–ด๋„ ๋ผ! ๊ทธ๋ฆฌ๊ณ  ์ˆซ์ž์•ผ
} = {
    name:"vini" //๊ทธ๋ž˜์„œ ๊ทธ ๊ฐ์ฒด๊ฐ€ ๋ญ”๋ฐ!?
}

if(player.age<10) //-> error! age๊ฐ€ undefined์ธ๊ฒƒ๊ฐ™์€๋ฐ?!
if(player.age && player.age<10) //ok to go
  • Allias Type (์ฝ”๋“œ ์žฌ์‚ฌ์šฉ)
type Player = { //type [๋Œ€๋ฌธ์ž]
    name:string, 
    age?:number 
}

const vini : Player = {
    name: "vini"
}

const bini : Player = {
    name: "bini",
    age: 28
}
  • ํ•จ์ˆ˜์—์„œ๋Š”?
    • ์ „๋‹ฌ์ธ์ž()๋’ท๋ถ€๋ถ„์— type ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
    function playerMaker(name:string) : Player {
        return {
            name
        }
    }
    
    const playerMaker = (name:string) : Player => ({name})
    
    const vini = playerMaker("vini") //ok to go
    vini.age = 28 //function ๋’ค์— :Player ๋ถ™์ด๋ฉด ok to go

2-4. Types of TS (2)

  • readonly
    • type ์ง€์ •ํ•ด์ค„ ๋•Œ ์•ž์— ์จ์ค˜์š”
    • ๋‹ค๋ฅธ๊ฑธ๋กœ ์ˆ˜์ •ํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•ด์ค˜์š” READ ONLY
    const names: readonly string[] = ["1","2"]
    //names.push ๋ถˆ๊ฐ€๋Šฅ!
  • Tuple
    • ์ •ํ•ด์ง„ ๊ฐฏ์ˆ˜์˜ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๊ณ  ⇒ 3๊ฐœ!
    • ์›ํ•˜๋Š” ์ˆœ์„œ์— ๋งž๋Š” ํƒ€์ž…์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค! ⇒ string, number, boolean์ˆœ์„œ๋Œ€๋กœ ํƒ€์ž…๋„ ์ง€์ผœ์„œ!
    const player: [string, number, boolean] = ["vini", 28, true]
  • any
    • TS์—์„œ ๋ฒ—์–ด๋‚˜๋Š” ์žฅ์น˜ (์ž์ฃผ ์“ฐ์ง€๋งˆ! ํƒˆ์ถœ๊ตฌ๋กœ ์“ฐ๊ณ  ์‹ถ์„๋•Œ๋งŒ!!)
    const a : any[] = [1,2,3,4] //any[]์ด๋‚˜ ๊ฐ€๋Šฅํ•ด์ง
    const b : any = true
    
    a+b //any๋ฅผ ์“ฐ๋ฉด ๊ฐ€๋Šฅํ•ด์ ธ๋ฒ„๋ฆผ.. JS๋ž‘ ๋‹ค๋ฅผ๊ฒŒ ์—†์–ด!

2-5. Types of TS (3)

  • unkwown
    • api์—์„œ response๊ฐ€ ์–ด๋–ค type์œผ๋กœ ์˜ฌ ๋•Œ ๋ชจ๋ฅผ ๋•Œ use
    • ๋ณ€์ˆ˜๊ฐ€ ๋ฌด์Šจ type์ธ์ง€ ๋ชจ๋ฅผ ๋•Œ use
    let a : unknown 
    
    if(typeof a === "number") {
        let b = a+1
    }
    
    if(typeof a === "string"){
        let b = a.toUpperCase();
    }
  • void
    • ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ๋จ
    • function์ด ์•„๋ฌด๊ฒƒ๋„ return์„ ์•ˆํ•  ๋•Œ use
    function hello(){
        console.log('x')
    }
  • never
    • function์ด never returnํ•˜์ง€ ์•Š๊ณ  error์„ throwํ•  ๋•Œ use
    function hello():never{
        throw new Error("xxx")
    }
    • type์ด ๋‘ ๊ฐ€์ง€์ผ ๋•Œ use
    function hello(name:string|number){
        if(typeof name==="string"){
            name //string
        } else if (typeof name === "number){
            name //number
            } else {
                name //never
            }
    }

 

'DEV_IN > TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

โ›”๏ธ ์ž์ฃผ ๋ณผ ์ˆ˜ ์žˆ๋Š” Errors  (0) 2023.02.17
TypeScript(apply)  (2) 2023.02.17
TypeScript(Interface)  (4) 2023.02.16
TypeScript(Classes)  (4) 2023.02.12
TypeScript (Function)  (3) 2023.02.09
Comments