TypeScript ์—ด๊ฑฐํ˜• + JavaScript "in"= ๐Ÿ‘

8322 ๋‹จ์–ด webdevtypescriptjavascript
cinch์—์„œ ์ €๋Š” ์žฌ๊ณ  ํŒ€์—์„œ ์ผํ•˜๋ฉฐ ์ˆ˜์ฒœ ๋Œ€์˜ ์ž๋™์ฐจ์— ๋Œ€ํ•œ ์ƒํƒœ, ์œ„์น˜ ๋ฐ ๊ธฐํƒ€ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ ํ•˜๋ฉด์„œ ๋ชจ๋“  ์ฐจ๋Ÿ‰ ์žฌ๊ณ ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ฝ”๋“œ์˜ ์ผ๋ถ€๊ฐ€ ์šฐ๋ฆฌ๊ฐ€ ๊ตฌ๋…ํ•˜๋Š” ํŠน์ • ์ด๋ฒคํŠธ์— ์‘๋‹ตํ•จ์„ ์˜๋ฏธํ•˜๋Š” ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ์ƒํƒœ์—์„œ ๋‹ค๋ฅธ ์ƒํƒœ๋กœ ์ž๋™์ฐจ๋ฅผ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๊ฒฐ์ •ํ•˜๋Š” ๋งŽ์€ ๋…ผ๋ฆฌ๊ฐ€ ํฌํ•จ๋œ ์ƒํƒœ ๋จธ์‹ /๋ชจ๋ธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฒ ์ด์Šค์—์„œ ๋ช‡ ๊ฐ€์ง€ ๋ฌด๊ฑฐ์šดif ์กฐ๊ฑด์„ ๋ฐœ๊ฒฌํ–ˆ์„ ๋•Œ ๋‹ค์Œ ํŒจํ„ด์„ ์ƒ๊ฐํ•ด ๋ƒˆ์Šต๋‹ˆ๋‹ค.

์ด ์˜ˆ์—์„œ๋Š” ๋งค์ผ ์ฐจ๋Ÿ‰๊ณผ ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๋กœ๋ด‡์„ ์ฃผ์ œ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! ๐Ÿš—

๋กœ๋ด‡์„ ์ฒ˜๋ถ„ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์›ํ•˜์ง€๋งŒ ์ƒํƒœ๊ฐ€ faulty ๋˜๋Š” damaged ์ธ ๊ฒฝ์šฐ์—๋งŒ ๋กœ๋ด‡์„ ์ฒ˜๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜์—์„œ ์—ด๊ฑฐํ˜•RobotStatus์„ ์„ค์ •ํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ๋กœ๋ด‡ ์ƒํƒœ๋ฅผ ๋‚˜์—ดํ•˜๊ณ  Robot ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋กœ๋ด‡์˜ ๋ชจ์–‘์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

enum RobotStatus {
    ready,
    damaged,
    faulty
}

interface Robot {
    name: string
    status: RobotStatus
}

function disposeRobot(robot: Robot): void {
    if (robot.status === RobotStatus.damaged || 
        robot.status === RobotStatus.faulty
    ) {
        console.log('Disposing of robot...')
    }

    console.log('Robot in incorrect state to dispose...')
}


์ง€๊ธˆ์€ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹ค๋ฅธ ์ƒํƒœ์— ๋Œ€ํ•ด ๋” ๋งŽ์€ ๊ฒ€์‚ฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์‹ญ์‹œ์˜ค. discontinued , dead , old , rusty , dangerous ์— ๋Œ€ํ•ด ์ข€ ๋” ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

enum RobotStatus {
    ready,
    damaged,
    faulty,
    discontinued,
    dead,
    old,
    rusty,
    dangerous
}

interface Robot {
    name: string
    status: RobotStatus
}

function disposeRobot(robot: Robot): void {
    if (robot.status === RobotStatus.damaged || 
        robot.status === RobotStatus.faulty ||
        robot.status === RobotStatus.discontinued ||
        robot.status === RobotStatus.dead ||
        robot.status === RobotStatus.old ||
        robot.status === RobotStatus.rusty ||
        robot.status === RobotStatus.dangerous ||
    ) {
        console.log('Disposing of robot...')
    }

    console.log('Robot in incorrect state to dispose...')
}


์ด์ œ if ๋ธ”๋ก์ด ๋‘๊บผ์›Œ์ง€๊ณ  ์•…์ทจ๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค. ๐Ÿ‘ƒ.
ํ—ˆ์šฉ๋œ ์ผํšŒ์šฉ ์ƒํƒœ๋ฅผ ํฌํ•จํ•˜๋Š” enum๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

enum DisposableStatus {
    damaged,
    faulty,
    discontinued,
    dead,
    old,
    rusty,
    dangerous
}


JavaScript์—๋Š” ์ง€์ •๋œ ์†์„ฑ์ด ์ง€์ •๋œ ๊ฐ์ฒด์— ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” in ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

prop in object


์ด๊ฒƒ์€ ์œ„์—์„œ if ๋ธ”๋ก์„ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์จ๋ณด์ž...

function disposeRobot(robot: Robot): void {
    if (robot.status in DisposableStatus) {
        console.log('Disposing of robot...')
    }

    console.log('Robot in incorrect state to dispose...')
}


TypeScript๋Š” ์—ด๊ฑฐํ˜•์„ ์ผ๋ฐ˜ JavaScript ๊ฐœ์ฒด๋กœ ์ปดํŒŒ์ผํ•˜๊ณ  ์ด๋Ÿฌํ•œ ์†์„ฑ์€ ๋Ÿฐํƒ€์ž„์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
์ด๊ฒƒ์€ ๋ฏธ๋ž˜์˜ ์ž์‹ ๊ณผ ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•ด ํ›จ์”ฌ ๋” ์ฝ๊ธฐ ์‰ฝ๊ณ  ์†Œํ™”ํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ