๊ธฐ๋ณธ ํƒ€์ž…

1๏ธโƒฃ ๊ธฐ๋ณธํƒ€์ž…: number, string, boolean, undefined, null

โœ”๏ธ number

let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
let big: bigint = 100n;


โœ”๏ธ string

let color: string = "blue";
color = "red";

let name: string = `april`;
let age: number = 35;
let sentence: string = `Hello, my name is ${name}.
 
I'll be ${age + 1} years old next month.`;


โœ”๏ธ boolean

let isDone: boolean = false;


โœ”๏ธ undefined

undefined: ๊ฐ’์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์ •ํ•ด์ง€์ง€ ์•Š์€ ์ƒํƒœ, ํ……ํ…… ๋น„์—ˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ๊ฒฐ์ •๋˜์ง€ ์•Š์€ ์ƒํƒœ

let name: undefined; // ๐Ÿ’ฉ์ด๋Ÿฐ์‹์œผ๋กœ ํ• ๋‹นํ•˜์ง„ ์•Š์Œ

let age: number | undefined; // number ๋˜๋Š” undefined์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค
age = undefined;
age = 21;


โœ”๏ธ null

null: ํ……ํ…… ๋น„์—ˆ๋‹ค

let person: null; // ๐Ÿ’ฉ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋Ÿฐ์‹์œผ๋กœ ํ• ๋‹นํ•˜์ง„ ์•Š์Œ

let person2: string | null;
person2 = null;
person2 = "yurim";



2๏ธโƒฃ ๊ธฐ๋ณธํƒ€์ž…: unknown, any, void, never, object

โœ”๏ธ ๐Ÿ’ฉunknown

unknown ์–ด๋–ค ํƒ€์ž…์ธ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค // ๐Ÿ’ฉ

let notSure: unknown = 0; // number๋ฅผ ํ• ๋‹น ํ–ˆ์Œ์—๋„
notSure = "hi"; // string์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค
notSure = true;


โœ”๏ธ ๐Ÿ’ฉany

any ์–ด๋–ค ๊ฒƒ์ด๋“  ๋‹ค ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค // ๐Ÿ’ฉ

let anything: any = 0; // number๋ฅผ ํ• ๋‹น ํ–ˆ์Œ์—๋„
anything = "hi"; // string์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค
anything = true;


โœ”๏ธ void

void ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด์ด ์—†์„ ๋•Œ์˜ ํƒ€์ž…. ์ƒ๋žต๋„ ๊ฐ€๋Šฅํ•˜๋‹ค

function print(): void {
  console.log("hello");
  return;
}
let unusable: void = undefined; // ๐Ÿ’ฉ undefined ๋ฐ–์— ํ• ๋‹น์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ


โœ”๏ธ never

never ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ฃฝ์œผ๋‹ˆ๊นŒ ๋ฆฌํ„ด๊ฐ’์ด ์ ˆ๋Œ€ ์—†๋‹ค. ๊ทธ๋Ÿด ๋•Œ never ํƒ€์ž…์„ ์ง€์ •. never ํƒ€์ž…์„ ์ง€์ •ํ–ˆ๋Š”๋ฐ ๋ฆฌํ„ด์ด ์žˆ๋‹ค๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ.

// throwError ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์—๋Ÿฌ ๋ฐœ์ƒ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜
function throwError(message: string): never {
  // message -> server (log)
  throw new Error(message);
  while (true) {}
}
let neverEnding: never; // ๐Ÿ’ฉ

// objet
let obj: object; // ๐Ÿ’ฉ
function acceptSomeObject(obj: object) {}
acceptSomeObject({ name: "ellie" });
acceptSomeObject({ animal: "dog" });


โœ”๏ธ object

object: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ํƒ€์ž…๊ณผ ๋™์ผ. ๊ฐ์ฒด ์†์„ฑ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž… ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์†์„ฑ ์ •๋ณด๋ฅผ ํฌํ•จํ•ด์„œ ํƒ€์ž…์„ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค(interface)๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

interface Person {
  name: string;
  age?: number; // ๋ฌผ์Œํ‘œ๊ฐ€ ๋“ค์–ด๊ฐ”๋‹ค๋Š” ๊ฒƒ์€, ์„ค์ •์„ ํ•ด๋„ ๋˜๊ณ  ์•ˆํ•ด๋„ ๋˜๋Š” ๊ฐ’์ด๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
}

const person: Person = {
  name: "april",
  age: 20,
};

// interface ๋ฅผ extends ํ•ด์„œ ์ƒ์†๋ฐ›๊ธฐ
interface Developer extends Person {
  skills: string[];
}

const expert: Developer = {
  name: "april",
  skills: ["typescript", "react"],
};

const people: Person[] = [person, expert];