๋ถ๋ฅ ์ ์ฒด๋ณด๊ธฐ126 ์ ๋ค๋ฆญ ์ ๋ค๋ฆญ(Generic) ํด๋์ค๋ ํจ์, ์ธํฐํ์ด์ค๋ฅผ ๋ค์ํ ํ์ ์ผ๋ก ์ฌ์ฌ์ฉ ๊ฐ๋ฅ ์ ์ธ ํ ๋๋ ํ์ ํ๋ผ๋ฏธํฐ๋ง ์์ฑํ๊ณ , ์์ฑํ๋ ์์ ์ ์ฌ์ฉํ๋ ํ์ ๊ฒฐ์ function getSize(arr:number[] | string[]):number{ return arr.length; } const arr1=[1,2,3]; getSize(arr1); const arr2=["a","b","c"]; getSize(arr2); //๋งค๊ฐ๋ณ์์ ํ์ ๋ง ๋ฐ๊พธ๊ณ ๋์ผํ ํจ์๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด, ํจ์ ์ค๋ฒ๋ก๋ ๋๋ ์ ๋์จ ํ์ ์ฌ์ฉํ๊ธฐ //๊ณ์ ํ์ ์ด ๋์ด๋๋ ๊ฒฝ์ฐ ์ ๋ค๋ฆญ ์ฌ์ฉ //์ฌ์ฉ: T๋ ์ด๋ค ํ์ ์ ์ ๋ฌ๋ฐ์์ ํด๋น ํจ์์์ ์ฌ์ฉ ํ ์ ์๋๋ก ํจ function getSize2(arr:T[]):number{ retu.. 2023. 1. 3. ํด๋์ค ํด๋์ค ํ์ ์คํฌ๋ฆฝํธ์์๋ ๋ฉค๋ฒ ๋ณ์๋ ๋ฏธ๋ฆฌ ์ ์ธํด์ฃผ์ด์ผ ํจ class Car{ color:string; //ํ์ ์คํฌ๋ฆฝํธ์์๋ ๋ฉค๋ฒ๋ณ์๋ ๋ฏธ๋ฆฌ ์ ์ธํด์ฃผ์ด์ผ ํจ constructor(color:string){ this.color=color; } start(){ console.log("start"); } } const bnw=new Car("red"); ๋ฉค๋ฒ ๋ณ์ ๋ฏธ๋ฆฌ ์ ์ธํ์ง ์๋ ๋ฐฉ๋ฒ -> ์ ๊ทผ ์ ํ์ ๋๋ readonlyํค์๋ ์ด์ฉ class Car2{ constructor(public color:string){ this.color=color; } start(){ console.log("start"); } } class Car3{ constructor(readonly color:string){ this... 2023. 1. 3. ๋ฆฌํฐ๋ด, ์ ๋์จ/๊ต์ฐจ ํ์ ๋ฆฌํฐ๋ด ํ์ (Literal Types) const: ๋ณํ์ง ์๋ ๊ฐ ์ ์ธ ํ ๋ let: ๋ณํ ์ ์๋ ๊ฐ ์ ์ธ ํ ๋ ๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์ : ์ ํด์ง string ๊ฐ์ ๊ฐ์ง ๊ฐ const userName1="Bob"; //๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์ : ์ ํด์ง string๊ฐ์ ๊ฐ์ง ๊ฐ let userName2="Tom"; let userName3:string | number="Tom"; //๊ฐ์ง ์ ์๋ ํ์ ๋ช ์ ํ๊ธฐ type Job="police"|"developer" |"teacher"; interface User{ name:string; job:Job; } const user: User = { name:"Bob", job:"developer", }; ์ ๋์จ ํ์ (Union Types) |(์ธ๋ก ์ค) inter.. 2023. 1. 3. ํจ์ ํจ์ ํํ function add(num1:number, num2:number){ return num1+num2; } function isAdult(age:number):boolean{ return age>10; } ํจ์์ ๋งค๊ฐ๋ณ์๋ optional๋ก ์ง์ ๊ฐ๋ฅ(๋จ, ํ์ ๋ช ํํ๊ฒ ๋ช ์ํด์ฃผ์ด์ผ ํจ) function hello(name?:string){ return `Hello, ${name || "world"}`; } const result=hello(); const result2=hello("Sam"); //์ฃผ์์ : optional์ธ ๋งค๊ฐ๋ณ์๋ ๋ค์ ๋ฐฐ์นํด์ผ ํจ function hello2(name:string, age?:number):string{ if(age!==undefined){ return `.. 2023. 1. 3. ์ด์ 1 ยทยทยท 19 20 21 22 23 24 25 ยทยทยท 32 ๋ค์