์ ์ฒด ๊ธ
์ฐ๋ ๋ ์์ฑ, ์ฐ๋ ๋ํ๋ง, Task ๋ช ์
using System; using System.Threading; using System.Collections.Generic; namespace ServerCore { class Program { static void MainThread() { Console.WriteLine("hello Thread!"); } static void Main(string[] args) { Thread t = new Thread(MainThread); t.Start(); //๋ณ๋์ ์ฐ๋ ๋๊ฐ ์คํ๋จ Console.WriteLine("Hello World!"); } } } ๋ฐฑ๊ทธ๋ผ์ด๋ ์ฐ๋ ๋ using System; using System.Threading; using System.Collections.Generic; name..
Union๊ณผ ๋ ๋ค๋ฅธ Intersection ํ์
Union์ด Or์ด๋ผ๋ฉด Intersection์ And & ์ผ ๊ฒ์ด๋ค^^ type Student = { name: string; score: number; }; type Worker = { employeeId: number; work: () => void; }; function internWork(person: Student & Worker) { console.log(person.name, person.employeeId, person.work()); } internWork({ name: "ํ๊ธธ๋", score: 86, employeeId: 6, work: ()=> {}, }); //ํ๊ธธ๋ 6 undefined ์์ ์ฝ๋๊ฐ ์๋์ ๊ฐ๋ค๋ฉด, type Student = { name: string; score..
ํ์ ํ์ ! Discriminated Union
์ฐจ๋ณํ(๊ตฌ๋ถ) ํ ์ ์๋ Union ์๋ ์ฝ๋๋ฅผ ๊ฐ์ ํด๋ณด์ type SuccessState = { response: { body: string; }; }; type FailState = { reason: string; }; type LoginState = SuccessState | FailState; function login(): SuccessState | FailState { return { response: { body: "logged in!", }, }; } function printLoginState(state: LoginState) { if ("response" in state) { console.log(`๐ ${state.response.body}`); } else { console.log..
ํ์ ์คํฌ๋ฆฝํธ 1์ฅ ์ฐ์ต๋ฌธ์ | ํ์ ์คํฌ๋ฆฝํธ์ ์ถ๋ก
1. ํ์ ์คํฌ๋ฆฝํธ์ ์ถ๋ก ๋ฐฉ์ ์๊ธฐ let a = 10423; //number let b = "apple"; //string const c = "grape"; //"grape" let d = [true, false, true]; //boolean[] let e = { ์ด๋ฆ: "smith" }; //{ ์ด๋ฆ: string } let f = [1, false, "ํ๊ธธ๋"]; //(number | boolean | string)[] const g = [3]; //number[] let h = null; //any 2. ์๋ฌ์ ์์ธ ํ์ ํ๊ธฐ a. let i: 3 = 3; i = 4; ~ //'4'ํ์์ '3'ํ์์ ํ ๋นํ ์ ์์ต๋๋ค. ์์ธ : i ๋ณ์๋ฅผ ๋ช ์์ ์ผ๋ก ์ค์ง ํ๋์ ๊ฐ์ ๋ํ๋ด๋ ํ์ ์ธ 'ํ์ ๋ฆฌํฐ๋ด..
์ง์ ํ ํ์ ์คํฌ๋ฆฝํธ์ ์์, Union ํ์
์ ๋์จ ํ์ ์ ํ์ ์คํฌ๋ฆฝํธ์์ ์ฝ๋ฉํ ๋ ๊ต์ฅํ ๋ง์ด ์ฐ๊ฒ ๋ ๊ฒ์ด๋ค. OR ๋๋ ํฉ์งํฉ์ผ๋ก ์ดํดํ๋ฉด ์ถฉ๋ถํ ๊ฒ์ด๋ค. type Direction = "left" | "right" | "up" | "down"; function move(direction: Direction) { console.log(direction); } type TileSize = 8 | 16 | 32; const tileA: TileSize = 16; //const tileB: TileSize = 10; //10 ํ์์ TileSize ํ์์ ํ ๋นํ ์ ์์ต๋๋ค. //function : login -> success, fail type SuccessState = { response: { body: string; }; }; type F..
ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฐ๋ ์ด์ , type alias์ String Literal Types
type alias(๋ด๊ฐ ์๋ก์ด ํ์ ์ ์ ์ํ ์ ์๋ค)๋ฅผ ์ด์ฉํ๋ฉด ๊ธฐ๋ณธ์ ์ธ ํ์ ๋ถํฐ ๋ณต์กํ๊ณ ์ ๊ตํ ํ์ ๊น์ง ์ ์ํ ์ ์๋ค. ๊ฐ๋ น type Text = string; ์์ ๊ฐ์ด ์์ฑํ๋ฉด Text๋ผ๋ ์๋ก์ด ํ์ ์ ๋ฌธ์์ด์ ๋งํ๋ค๊ณ ์ง์ ํด์ฃผ๋ ๊ฒ์ด๋ค. type Text = string; const name: string = "ํ๊ธธ๋"; const nickName: Text = "ํฉ์ง์ด"; console.log(name); //ํ๊ธธ๋ console.log(nickName); //ํฉ์ง์ด type alias ๋ฅผ ํตํด์ ๋ค์๊ณผ ๊ฐ์ด ํน์ ํ๋กํผํฐ๊ฐ ๊ฐ์ ๋ ๊ฐ์ฒด ํ์ ์ ๋ง๋ค ์๋ ์๋ค. ๊ทธ๋ฆฌ๊ณ ๋ณ์์ ํ์ ์ type alias๋ก ์์ฑํ ๊ฒ์ผ๋ก ์ง์ ํด์ฃผ๋ฉด ํน์ ํ๋กํผํฐ๋ง ํ ๋นํ ์ ์์ ๋ฟ๋๋ฌ ํน์ ํ๋กํผํฐ์ ํ..
์คํ๋ ๋ ์ฐ์ฐ์, ๋ฐฐ์ด, ํํ
์คํ๋ ๋ ์ฐ์ฐ์์ ํจ๊ป ์ฐ๋ ๊ฒฝ์ฐ function addNum(...numbers: number[]): number { return numbers.reduce((a, b) => a + b); } console.log(addNum(1, 2)); console.log(addNum(1, 2, 3)); console.log(addNum(1, 2, 3, 4)); 3 6 10 ๋ฐฐ์ด๊ณผ ํํ nst fruits: string[] = ["์ฌ๊ณผ", "๋ฐ๋๋"]; const numbers: Array = [1, 2, 3, 4, 5]; const cloths: [string, number] = ['์ฒญ๋ฐ์ง', 2]; console.log(cloths[0]); //์ฒญ๋ฐ์ง ํํ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ์ง ์๋๋ค. const cloth..