๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ฌ/ใ…ใ……ใ…Œใ…‹ใ…ใ…… ์ฑŒ๋ฆฐ์ง€

24์ผ์ฐจ

๐Ÿฆ„

24์ผ์ฐจ

 

Part 4. TypeScript Essentials

Ch 1. TypeScript

Ch 2. Basic Types

 


 

 

Ch1. TypeScript

 

 

Typescript๋Š” ์œ ํ˜•์„ ์ถ”๊ฐ€ํ•˜์—ฌ JavaScript๋ฅผ ํ™•์žฅํ•œ๋‹ค.
Typescript๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ์˜ค๋ฅ˜๋ฅผ ํฌ์ฐฉํ•˜๊ณ  ์ˆ˜์ • ์‚ฌํ•ญ์„ ์ œ๊ณตํ•˜๋Š” ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๐Ÿ”— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ๊ธ€๋กœ๋ฒŒ๋กœ ์„ค์น˜ ํ›„,

// ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ ๊ธ€๋กœ๋ฒŒ ์„ค์น˜
$ npm i typescript -g

// cli ๋ช…๋ น์–ด๋กœ ํŒŒ์ผ ์ปดํŒŒ์ผ
$ tsc test.ts

// ํŠน์ • ํ”„๋กœ์ ํŠธ ํด๋”์—์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ ์„ค์ •์— ๋งž์ถฐ ์ปดํŒŒ์ผ
$ tsc --init
$ tsc

 

๐Ÿ”— ํ”„๋กœ์ ํŠธ์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ์„ค์น˜ ํ›„,

// ํ”„๋กœ์ ํŠธ์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ ์„ค์น˜
$ npm i typescript

// .bin ์•ˆ์˜ ๋ช…๋ น์–ด๋กœ ํŒŒ์ผ ์ปดํŒŒ์ผ (node_modules/.bin/tsc)
$ npx tsc --init
$ npx tsc

// npm ์Šคํฌ๋ฆฝํŠธ๋กœ ํŒŒ์ผ ์ปดํŒŒ์ผ

// ํ”„๋กœ์ ํŠธ์— ์žˆ๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์„ค์ •์— ๋งž์ถฐ, npm ์Šคํฌ๋ฆฝํŠธ๋กœ ์ปดํŒŒ์ผ

 

 

 ๐Ÿ”— TypeScript Annotation 

let a: number;
a = 39;
a = "Mark"; // Error

function hello(b: number) {}
hello(39);
hello("Mark"); // Error

 


 

 

Ch2. Basic Types

 

 

  •  Primitive Types 
    • boolean
    • number
    • string
    • symbol
      • primitive ํƒ€์ž…์˜ ๊ฐ’์„ ๋‹ด์•„์„œ ์‚ฌ์šฉํ•œ๋‹ค.
      • ๊ณ ์œ ํ•˜๊ณ  ์ˆ˜์ • ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฐ’์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
      • ๊ทธ๋ž˜์„œ ์ฃผ๋กœ ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๋Š”๋ฐ ์“ฐ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.
    • null & undefined
      • null ์ด๋ผ๋Š” ํƒ€์ž…์€ null ์ด๋ผ๋Š” ๊ฐ’๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
      • ๋Ÿฐํƒ€์ž„์—์„œ typeof ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์•Œ์•„๋‚ด๋ฉด null์€ object, undefined๋Š” undefined์ด๋‹ค.
  • object
    • "primitive ํƒ€์ž…์ด ์•„๋‹Œ ๊ฒƒ"์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํƒ€์ž…
  • Array
    • ๋ฐฐ์—ด ์•ˆ์— ์š”์†Œ๋“ค์€ ๋ชจ๋‘ ๊ฐ™์€ ํƒ€์ž…์ด๋‹ค.
  • Tuple
    • ๊ธธ์ด๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋‹ค.
    • ํƒ€์ž…์ด ์ •ํ™•ํ•˜๋‹ค.
  • any
    • ์–ด๋–ค ํƒ€์ž…์ด์ด์–ด๋„ ์ƒ๊ด€์—†๋Š” ํƒ€์ž…์ด๋‹ค.
    • ์ตœ๋Œ€ํ•œ ์“ฐ์ง€ ๋ง๊ธฐ~~~
  • unknown
    • ์ด ๋ณ€์ˆ˜๊ฐ€ ๋ฌด์—‡(ํƒ€์ž…)์ด๋“  ๋  ์ˆ˜ ์žˆ์Œ์„ ์•Œ๋ ค์ค€๋‹ค.
  • never
    • never ์—๋Š” ๊ทธ ์–ด๋–ค ๊ฒƒ๋„ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.
    • ์ž˜๋ชป๋œ ํƒ€์ž…์„ ๋„ฃ๋Š” ์‹ค์ˆ˜๋ฅผ ๋ง‰๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค.
  • void
    • undefined๋ฅผ return

 

// boolean.ts

let isDone: boolean = false;
isDone = true;
console.log(typeof isDone); // boolean

let isOk: Boolean = true;
let isNotOk: boolean = new Boolean(true); // Error !boolean ๋Œ€๋ฌธ์ž๋กœ!

 

// symbol.ts

console.log(Symbol("foo") === Symbol("foo")); // false

const sym = Symbol();
const obj = {
  [sym]: "value",
};
obj[sym];

 

// object.ts

const person1 = { name: "Mark", age: 39 };

// declare function create(o: object | null): void;
const person2 = Object.create({ name: "Mark", age: 39 });

 

// array.ts

let list: (number | string)[] = [1, 2, 3, "4"];

// let list: Array<number> = [1, 2, 3];

 

// tuple.ts

let x: [string, number];
x = ["hello", 39];
x = [10, "Mark"]; // Error
x[2] = "world"; // Error

const person: [string, number] = ["Mark", 39];
const [first, second, third] = person; // first: string, second: number, third Error

 

// any.ts

function returnAny(message: any): any {
  console.log(message);
}
const any1 = returnAny("๋ฆฌํ„ด์€ ์•„๋ฌด๊ฑฐ๋‚˜");
any1.toString();
let looselyTyped: any = {};

// any๋Š” ๊ณ„์†ํ•ด์„œ ๊ฐœ์ฒด๋ฅผ ํ†ตํ•ด ์ „ํŒŒ๋œ๋‹ค.
// ๊ฒฐ๊ตญ, ๋ชจ๋“  ํŽธ์˜๋Š” ํƒ€์ž… ์•ˆ์ „์„ฑ์„ ์žƒ๋Š” ๋Œ€๊ฐ€๋กœ ์˜จ๋‹ค.
const d = looselyTyped.a.b.c.d;

function leakingAny(obj: any) {
  const a: number = obj.num; // ๋ˆ„์ˆ˜ ๋ง‰๊ธฐ!
  const b = a + 1;
  return b;
}
const c = leakingAny({ num: 0 });
c.indexOf("0"); // Error

 

// unknown.ts

declare const maybe: unknown;

const aNumber: number = maybe; // Error

if (maybe === true) {
  const aBoolean: boolean = maybe; // ํƒ€์ž… ๊ฐ€๋“œ๋ฅผ ํ†ตํ•ด์„œ ํƒ€์ž…์„ ํ•œ์ • ์‹œํ‚ด.
  const aString: string = maybe; // Error
}

if (typeof maybe === "string") {
  const aString: string = maybe;
  const aBoolean: boolean = maybe; // Error
}

 

// never.ts

function error(message: string): never {
  throw new Error(message);
}

function fail() {
  return error("failed");
}

function infiniteLoop(): never {
  while (true) {}
}

let a: string = "hello";
if (typeof a !== "string") {
  a; // a: never
}

declare const b: string | number;
if (typeof b !== "string") {
  a; // b: number
}

 

// void.ts

// returnVoid: void
function returnVoid(message: string): void {
  console.log(message);

  return undefined;
}

returnVoid("๋ฆฌํ„ด์ด ์—†๋‹ค.");

 

'๐Ÿ’ฌ > ใ…ใ……ใ…Œใ…‹ใ…ใ…… ์ฑŒ๋ฆฐ์ง€' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

26์ผ์ฐจ  (0) 2022.03.16
25์ผ์ฐจ  (0) 2022.03.15
23์ผ์ฐจ  (0) 2022.02.26
22์ผ์ฐจ  (0) 2022.02.25
21์ผ์ฐจ  (0) 2022.02.20