๐Ÿ“˜ TypeScript

    ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ˆ™์ œ Part 2 (5~9) : ํด๋ž˜์Šค, ์˜ค๋ธŒ์ ํŠธ ํƒ€์ž…์ง€์ •

    Car ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ { model : '์†Œ๋‚˜ํƒ€', price : 3000 } ์ด๋ ‡๊ฒŒ ์ƒ๊ธด object๋ฅผ ๋ณต์‚ฌํ•ด์ฃผ๋Š” class๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณต์‚ฌ๋œ object ์ž๋ฃŒ๋“ค์€ .tax() ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ๋ฐ ํ˜„์žฌ object์— ์ €์žฅ๋œ price์˜ 10๋ถ„์˜1์„ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. model๊ณผ price ์†์„ฑ๊ณผ tax() ํ•จ์ˆ˜์˜ return ํƒ€์ž…์ง€์ • ํ•˜์ž. class Car { model: string; price: number; constructor(model: string, price: number) { this.model = model; this.price = price; } tax() :number{ return this.price / 10; } } const morning = new Car..

    [์ฝ”๋”ฉ์• ํ”Œ] ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ class : constructor, prototype ํƒ€์ž…์ง€์ •

    ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ class์˜ constructor ์‚ฌ์šฉํ•˜๊ธฐ ์ผ๋ฐ˜์ ์ธ JavaScript ํด๋ž˜์Šค ๋ฐ ๊ฐ์ฒด ์ƒ์„ฑ์˜ ์˜ˆ์‹œ์ด๋‹ค. // JavaScript class Candidate { constructor(personality, age, gender) { this.personality = personality; this.age = age; this.gender = gender; } } const candidate1 = new Candidate('good', 28, 'woman'); JavaScript์—์„œ๋Š” ํƒ€์ž… ์ •๋ณด๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋•Œ๋ฌธ์— ์œ„์ฒ˜๋Ÿผ๋งŒ ํ‘œ๊ธฐํ•ด๋„ ๋˜์ง€๋งŒ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๊ฐ ์†์„ฑ๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜์— ํƒ€์ž…์ง€์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. // TypeScript class Candidate { ..

    [์ฝ”๋”ฉ์• ํ”Œ] ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ HTML ์กฐ์ž‘์‹œ ์ฃผ์˜์  (2) : ๋” narrowingํ•˜๊ธฐ

    ์šฐ์„ .. { "compilerOptions": { ... "strictNullChecks": true, ... }, } tsconfig.json ํŒŒ์ผ์—์„œ strictNullCheck ์˜ต์…˜์„ true๋กœ ๋ฐ”๊พผ๋‹ค. (ํ˜น์€ "strict" : true๋กœ ์จ๋‘ฌ๋„ ๋œ๋‹ค) ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด TypeScript ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋” ์—„๊ฒฉํ•œ ๊ทœ์น™์„ ์ ์šฉํ•˜๋ฉฐ, ๋ณ€์ˆ˜์— ๋Œ€ํ•œ null ์ฒดํฌ๊ฐ€ ๊ฐ•์ œ๋œ๋‹ค. href ์กฐ์ž‘์‹œ ์˜ค๋ฅ˜ let link = document.querySelector('#link'); if(link instanceof HTMLElement){ link.href = 'https://kakao.com'; } HTMLElement ํƒ€์ž…์€ href ์†์„ฑ์ด ์—†๋‹ค๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋œฌ๋‹ค. ๋งํฌํƒœ๊ทธ์˜ ๊ฒฝ์šฐ์—๋Š” ์ข€๋” ์ข๊ฒŒ narrowingํ•ด..

    [์ฝ”๋”ฉ์• ํ”Œ] ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ HTML ์กฐ์ž‘์‹œ ์ฃผ์˜์  (1)

    ์šฐ์„ .. { "compilerOptions": { ... "strictNullChecks": true, ... }, } tsconfig.json ํŒŒ์ผ์—์„œ strictNullCheck ์˜ต์…˜์„ true๋กœ ๋ฐ”๊พผ๋‹ค. (ํ˜น์€ "strict" : true๋กœ ์จ๋‘ฌ๋„ ๋œ๋‹ค) ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด TypeScript ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋” ์—„๊ฒฉํ•œ ๊ทœ์น™์„ ์ ์šฉํ•˜๋ฉฐ, ๋ณ€์ˆ˜์— ๋Œ€ํ•œ null ์ฒดํฌ๊ฐ€ ๊ฐ•์ œ๋œ๋‹ค. HTML ์กฐ์ž‘์‹œ ์˜ค๋ฅ˜ let title = document.querySelector('#title'); title.innerHTML = '์•ˆ๋…•ํ•˜์„ธ์š”'; // title is possibly Element | null TypeScript์—์„œ strictNullChecks ์˜ต์…˜์ด ํ™œ์„ฑํ™”๋œ ๊ฒฝ์šฐ, ์œ„ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์€ Element | ..

    [์ฝ”๋”ฉ์• ํ”Œ] ํ•จ์ˆ˜์™€ object ๋ฉ”์„œ๋“œ์— type alias ์ง€์ •ํ•˜๊ธฐ

    ํ•จ์ˆ˜ type alias ํ•จ์ˆ˜ ํƒ€์ž…๋„ type alias๋กœ ์ €์žฅํ•ด์„œ ์“ธ ์ˆ˜ ์žˆ๋‹ค. type NumOut = (x : number, y : number ) => number let ABC :NumOut = function(x,y){ return x + y } function ํ‚ค์›Œ๋“œ์—๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ๋‚ด๋ถ€๋‚˜ {} ๋ฐ”๋กœ ์™ผ์ชฝ์—๋งŒ ํƒ€์ž…์ง€์ •์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— function ํ•จ์ˆ˜์ด๋ฆ„ :NumOut (){} ํ˜•ํƒœ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์–ด์„œ ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•œ๋‹ค. *** ์ฃผ์˜์‚ฌํ•ญ : ํ•จ์ˆ˜ type alias๋ฅผ ๋ถ€์ฐฉํ•˜๋ ค๋ฉด ํ•จ์ˆ˜ํ‘œํ˜„์‹์œผ๋กœ ์จ์•ผ ํ•œ๋‹ค. object ๋ฉ”์„œ๋“œ type alias type Member = { name : string, age : number, plusOne : ( x :number ) => number, c..

    [์ฝ”๋”ฉ์• ํ”Œ] ํƒ€์ž…์„ ํŠน์ • ๊ฐ’์œผ๋กœ ์ง€์ •ํ•˜์ž : Literal Types (+ as const)

    Literal Types ํƒ€์ž…์ง€์ •์„ ๋”์šฑ strictํ•˜๊ฒŒ ํŠน์ • ๊ธ€์ž๋‚˜ ์ˆซ์ž๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ฒŒ ์ œํ•œ์„ ๋‘˜ ์ˆ˜ ์žˆ๋‹ค. // ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ function greet(name: "Alice" | "Bob"): "Hello, Alice!" | "Hello, Bob!" { return `Hello, ${name}!`; } // ์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ function multiply(a: 2 | 3, b: 2 | 3): 6 { return a * b; } // ๋ถˆ๋ฆฌ์–ธ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ ๋ฆฌํ„ด๊ฐ’ function isAdmin(userType: "admin" | "regular"): boolean { return userType === "admin"; } // ์‚ฌ์šฉ ์˜ˆ์‹œ consol..