02. ๐ฆ ํ์ ์คํฌ๋ฆฝํธ ์ค์ ํ์ฉ ์์ ์ ๋ฆฌ
- ๋น ๋ฐฐ์ด์ ํ์ : never[]
๐ 1. ๋น ๋ฐฐ์ด์ ํ์ : never[]
- ์ด๊ธฐ๊ฐ ์์ด ์ ์ธํ ๋น ๋ฐฐ์ด์ ํ์ ์ด never[]๋ก ์ถ๋ก ๋จ.
- ์ดํ array.push(โhelloโ)๋ฅผ ํ๋ฉด ํ์ ์คํฌ๋ฆฝํธ๋ โhelloโ์ ํ์ ์ ๊ธฐ์ค์ผ๋ก ๋ฐฐ์ด์ ์๋์ผ๋ก string[] ๋ฑ์ผ๋ก ์ถ๋ก ํจ.
- ํ์ง๋ง ๊ฐ๋ฅํ๋ฉด ์ด๊ธฐ ํ์ ์ ๋ช ํํ ์ง์ ํ๋ ๊ฒ์ด ์์
const array = [];
array.push("hello");
const array: string[] = [];
๐ 2. querySelector์ DOM ์์ ํ์
์ฆ, ์ ํ์๊ฐ DOM์ ์์ ์๋ ์๋ค๋ ์ ์ ๊ณ ๋ คํด์ผ ํจ โ ์กฐ๊ฑด๋ฌธ์ผ๋ก null ์ฒดํฌ ํ์
const head: Element = document.querySelector("#head")
document.querySelector๋ ๋ฆฌํด ํ์
์ด Element | null
โ์ฃผ์: Non-null ๋จ์ธ ์ฐ์ฐ์ !
- !๋ โ์ ๋ null ์๋โ์ ๊ฐ์ ๋ก ์ ์ธํ๋ ๋ฐฉ์.
- ํ์ง๋ง ์ค์ null์ผ ๊ฒฝ์ฐ ๋ฐํ์ ์๋ฌ ๋ฐ์ ๊ฐ๋ฅ โ โ ์ถ์ฒํ์ง ์์
if (head) {
head.innerHTML = "hello world";
}
const head2: Element = document.querySelector("#head")!;
๐ 3. ์์ ํ์ vs ์์ ๋ํผ ํ์
์์ ํ์ (Primitive Type) ์ด๋?
์์ ํ์ ์ ์๋ฐ์คํฌ๋ฆฝํธ์์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ด ๋๋ ๋ฐ์ดํฐ ํ์ ์ผ๋ก, ๊ฐ ์์ฒด๋ฅผ ์ ์ฅํ๊ณ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ(immutable) ํ ์ฑ์ง์ ๊ฐ์ง๋๋ค.
์์ | ํ์ ์ค๋ช |
---|---|
string | ๋ฌธ์์ด (โhelloโ) |
number | ์ซ์ (42, 3.14) |
boolean | ๋ถ๋ฆฌ์ธ (true, false) |
null | ๊ฐ์ด ์์ |
undefined | ์ ์๋์ง ์์ |
bigint | ํฐ ์ ์ (123n) |
symbol | ์ ์ผํ ๊ฐ ์๋ณ์ (Symbol(โidโ)) |
string (์์ ํ์ )
- string์ **์์ ํ์ (primitive type)**์ผ๋ก, ๊ฐ ์์ฒด๊ฐ ๋ฌธ์์ด์
- ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ด ์ข๊ณ , ๋น๊ต๋ ๊ฐ ์์ฒด๋ก ํจ (===๋ก ๋น๊ต ์ ์ ํํจ)
- ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์์๋ก String ๊ฐ์ฒด๋ก ๊ฐ์ธ๊ธฐ ๋๋ฌธ์ ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
- โ ํ์ ์คํฌ๋ฆฝํธ์์๋ ํญ์ ์ด๊ฑธ ์ฌ์ฉ
String (์์ ๋ํผ ํ์ )
- String์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ํผ ๊ฐ์ฒด(wrapper object)
- ๋ด๋ถ์ ์ผ๋ก ๋ฌธ์์ด์ ๊ฐ์ผ ๊ฐ์ฒด(Object) ํํ๋ก ๋์
- typeof b๋ฅผ ์ฐ์ผ๋ฉด โobjectโ๋ก ๋์ด
- ๐ซ b === โhellโ ์ false โ ๊ฐ์ด ์๋ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๊ธฐ ๋๋ฌธ
- โ ํ์ ์คํฌ๋ฆฝํธ์์ String, Number, Boolean์ ์ง์ (ํผ๋, ๋น๊ต ์ค๋ฅ ๋ฐ์ ๊ฐ๋ฅ์ฑ ๋์)
const a: string = "hello"
const b: String = new String("hello")
console.log(typeof a) // "string"
console.log(typeof b) // "object"
console.log(a === b) // false
string์ ๊ธฐ๋ณธ ํ์
(primitive)
๐ 4. ๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์ & ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด ํ์
๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์ (String Literal Type)
- World๋ ๋ฌธ์์ด ๋ฆฌํฐ๋ด ์ ๋์จ ํ์
- World๋ โworldโ ๋๋ โhellโ ๋ ๊ฐ ์ค ํ๋๋ง ํ์ฉ
// ๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์
type hello = "hello";
// ๋ฌธ์์ด ์ ๋์จ ๋ฆฌํฐ๋ด ํ์
type World = "world" | "hell";
const z: World = "world";
const x: World = "heaven"; // โ ์ค๋ฅ
ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด ํ์ (Template Literal Type)
- Greeting์ template literal type์ ์ฌ์ฉํ์ฌ โhello worldโ ๋๋ โhello hellโ์ฒ๋ผ ์๋์ผ๋ก ์์ฑ๋จ
- ์ด ๋ฐฉ์์ ์ ํํ๋ ๋ฌธ์์ด ๊ฐ์ ๊ฐ๋ ฅํ๊ฒ ํ์ ์ผ๋ก ์ ํํ ๋ ์ ์ฉ
- โ Ctrl + Space ๋ก ๊ฐ๋ฅํ ๋ฌธ์์ด ์๋์์ฑ ํํธ๋ฅผ ๋ฐ์ ์ ์์
type Greeting = `hello ${World}`;
const te: Greeting = ""; // ์๋์์ฑ ์ถ์ฒ ๊ฐ๋ฅ: "hello world" | "hello hell"
๐ฆ ์ค์ ์์
type HTTPMethod = "GET" | "POST";
type Endpoint = "users" | "posts";
type APIRoute = `${HTTPMethod} /api/${Endpoint}`;
const route1: APIRoute = "GET /api/users"; // โ
const route2: APIRoute = "POST /api/posts"; // โ
const route3: APIRoute = "DELETE /api/users"; // โ Error
๐ 5. ํํ(Tuple)์์ ์ฃผ์ํ ์
-
ํํ์ ๊ณ ์ ๋ ๊ธธ์ด์ ์์, ํ์ ์ ๊ฐ์ง ๋ฐฐ์ด์ด์ง๋ง,
-
push๋ ๋ฐํ์ ๋ ๋ฒจ์์ ๋งํ์ง ์์
-
ํ์ ์์ ์ฑ์ ๋์ด๋ ค๋ฉด as const ๋๋ ํํ์ ์กฐ์ํ์ง ์๋๋ก ์ค๊ณ ํ์
-
์ค๋ฅ ๋ฐ์ -ํํ์ ๊ธธ์ด๊ฐ ๊ณ ์ ๋จ
-
๋งํ์ง ์์ -์๋ฐ์คํฌ๋ฆฝํธ ๋์์ ํ์ฉ๋จ
const tuple: [string, number] = ["1", 1];
tuple[2] = "hello";
tuple.push("hello");
ํญ๋ชฉ | ์ค๋ช |
---|---|
๋น ๋ฐฐ์ด์ ํ์ | ์ด๊ธฐ์๋ never[] ๋ก ์ถ๋ก ๋๋ฉฐ, push ํ ํ์
๊ฒฐ์ ๋จ |
querySelector | ๋ฆฌํด ํ์
์ด Element or null , ์กฐ๊ฑด๋ฌธ์ผ๋ก null ์ฒดํฌ ํ์ |
Non-null ๋จ์ธ ! | null์ด ์๋์ ๊ฐ์ ๋ก ๋ณด์ฅํ๋, ๋ฐํ์ ์ค๋ฅ ์ํ ์์ |
string vs String | string ์ ๊ธฐ๋ณธ ํ์
, String ์ ๊ฐ์ฒด โ string ๋ง ์ฌ์ฉ |
๋ฆฌํฐ๋ด ์ ๋์จ ํ์ | โworldโ or โhellโ ์ฒ๋ผ ๊ฐ ์์ฒด๋ฅผ ํ์ ์ผ๋ก ์ ํ ๊ฐ๋ฅ |
ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด ํ์ | hello ${World} ์ฒ๋ผ ๋ฌธ์์ด ์กฐํฉ์ ํ์
์ผ๋ก ์์ฑ |
ํํ์ push ๋ฌธ์ | ํํ์ ๊ณ ์ ๋ ๊ธธ์ด์ด๋, push ๋ ๋งํ์ง ์์ โ ์ฃผ์ ํ์ |
Last updated on