Skip to content

Latest commit

ย 

History

History
417 lines (286 loc) ยท 9.69 KB

basic.md

File metadata and controls

417 lines (286 loc) ยท 9.69 KB

Basic

TypeScript

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์ •์  ํƒ€์ž… ์–ธ์–ด์ด๋ฉฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ํƒ€์ž… ์‹œ์Šคํ…œ์„ ์ ์šฉํ•ด ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค.

ํƒ€์ž… ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.

  • ์—๋Ÿฌ๋ฅผ catch ํ•˜๋Š” ๊ฒƒ์„ ๋„์™€์ค€๋‹ค.
  • Type annotation์„ ํ†ตํ•ด ํ”„๋กœํผํ‹ฐ๋“ค์— ๋Œ€ํ•œ ๋ฌ˜์‚ฌ์™€ ์ฝ”๋ฉ˜ํŠธ ์ œ๊ณต
  • ์ฝ”๋“œ ์ž‘์„ฑ์‹œ์—๋งŒ active

Process

  • TypeScript code โ†’ TypeScript complierโ†’ Plain old JavaScript
  • ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์•„๋‹Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์‹คํ–‰ํ•œ๋‹ค.

Type

ํƒ€์ž…์ด๋ž€ ํ”„๋กœํผํ‹ฐ์™€ ๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•œ ๋ฐฉ์‹์ด๋‹ค.

์›์‹œ ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • number, boolean, void, undefined, string, symbol, null

๊ฐ์ฒด ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • functions, arrays, classes, objects

๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ํƒ€์ž…์— ์‹ ๊ฒฝ์จ์•ผํ• ๊นŒ?

Why do we care about types?

๊ทธ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ํƒ€์ž…๋“ค์€ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์—๋Ÿฌ๋ฅผ ์œ„ํ•ด ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ• ๋•Œ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • ํƒ€์ž…์€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ž‘์„ฑํ•œ ์ฝ”๋“œ์˜ ์ดํ•ด๋ฅผ ๋„์šธ ์ˆ˜ ์žˆ๋‹ค.


ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ํƒ€์ž…์€ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ๋ชจ๋“  ๊ณณ์—์„œ ์‚ฌ์šฉ๋˜์–ด์•ผํ•œ๋‹ค.


Type Inference and Type Annotation

Type Inference

Typescript tries to figure out what type of value a variable refers to

type infrenece๋ž€ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž๋™์œผ๋กœ ํƒ€์ž…์„ ์ถ”์ธกํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

When to use ? Always!


Type Annotation

Code we add to tell Typescript what type of value a variable will refer to

type annotation์ด๋ž€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๋ช…์‹œ์ ์œผ๋กœ ๊ฐ’์ด ๊ฐ€๋ฅดํ‚ค๋Š” ํƒ€์ž…์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

When to use?

  • ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๋‹ค๋ฅธ ๋ผ์ธ์—์„œ ์ดˆ๊ธฐํ™”ํ• ๋•Œ
  • Type inference๋กœ ์ •ํ™•ํ•œ ํƒ€์ž…์ด ์ฃผ์–ด์ง€์ง€ ์•Š์„๋•Œ
  • any ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • ๊ฐ’์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•  ํ•„์š”๊ฐ€ ์žˆ์„๋•Œ
const num1: number = 5; // number type์œผ๋กœ type annotation
const str: string = 'Hi';

let employee : { 
    id: number; 
    name: string; 
}; 
employee = { 
  id: 100, 
  name : "John"
}
const json = `{"x": 10, "y": 20}`;

// type annotation์„ ํ•˜์ง€ ์•Š์•˜์„๋•Œ
const coord = JSON.parse(json);
coord.abc; // undefined, coord๊ฐ€ abc๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€์•Š์œผ๋ฏ€๋กœ undefiend

// type annotation์„ ํ–ˆ์„๋•Œ
const coordTyped: {x:number; y:number} = JSON.parse(json);
coord.abc;  // error
// Type inference๋กœ ์ •ํ™•ํ•œ ํƒ€์ž…์ด ์ฃผ์–ด์ง€์ง€ ์•Š์•„ error
let numbers = [-10, -1, 12];
let numberAboveZero = false;

for(let i = 0 ; i < numbers.length; i++) {
	if(numbers[i] > 0) {
		numberAboveZero = numbers[i]; 
		// error, type inference๋กœ ์ •ํ™•ํ•œ ํƒ€์ž…์„ ์•Œ ์ˆ˜ ์—†์Œ.
	}
}

// Type annotation
let numbers = [-10, -1, 12];
let numberAboveZero: boolean | number = false;

for(let i = 0 ; i < numbers.length; i++) {
	if(numbers[i] > 0) {
		numberAboveZero = numbers[i]; 
		// error, type inference๋กœ ์ •ํ™•ํ•œ ํƒ€์ž…์„ ์•Œ ์ˆ˜ ์—†์Œ.
	}
}
// ๋น„๊ตฌ์กฐ์ (unstructured) annotation
const profile = {
	name:'alex',
	age: 20
}

const { age }: {age: number} = profile;

๊ธฐํƒ€ ๋‚ด์šฉ๋“ค ์ •๋ฆฌ

Types

tuple

tuple์€ ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๊ฐ ์—˜๋ฆฌ๋จผํŠธ๋“ค์˜ ํƒ€์ž…์„ ๋ช…์‹œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

let x: [string, number];
x = ["hello", 2];

enum

enum์€ ์ˆซ์ž ๋ฐ์ดํ„ฐ์…‹์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š” ํƒ€์ž…์ด๋‹ค.

enum Color {
  Red = 1,
  Green,
  Blue,
}
let c: Color = Color.Green;

any

์•Œ์ง€๋ชปํ•˜๋Š” ๋ณ€์ˆ˜ ์œ ํ˜•์„ ์„ค๋ช…ํ•˜๋Š” ํƒ€์ž…์ด๋‹ค. ์ปดํŒŒ์ผ์‹œ ํƒ€์ž…๊ฒ€์‚ฌ๋Š” ํ•˜์ง€ ์•Š๋Š”๋‹ค.

never

์ ˆ๋Œ€๋กœ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฐ’์˜ ํƒ€์ž…์ด๋‹ค. ํ•ญ์ƒ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ฑฐ๋‚˜ ๋ฐ˜ํ™˜ํ•˜์ง€์•Š๋Š” ํ•จ์ˆ˜ ํ‘œํ˜„์‹์ด๋‚˜ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ์œ ํ˜•์— ์‚ฌ์šฉ๋œ๋‹ค.

// Function returning never must not have a reachable end point
function error(message: string): never {
  throw new Error(message);
}

// Inferred return type is never
function fail() {
  return error("Something failed");
}

// Function returning never must not have a reachable end point
function infiniteLoop(): never {
  while (true) {}
}

Type Array

type array๋ž€ ๊ฐ ์š”์†Œ๊ฐ€ ํŠน์ •ํ•œ ํƒ€์ž…์˜ ๊ฐ’์ธ ๋ฐฐ์—ด์ด๋‹ค.

์™œ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”๊ฐ€?
  • ๋‹ค๋ฅธ ๋ฐฐ์—ด ์š”์†Œ๋“ค์˜ ํƒ€์ž…๊ณผ ๋งž์ง€์•Š๋Š”(imcompatible) ๊ฐ’์ด ๋ฐฐ์—ด์— ์ถ”๊ฐ€๋˜๋Š”๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.
  • map, forEach, reduce ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ• ๋•Œ ๋„์›€์„ ์ค€๋‹ค.
  • ๋ฐฐ์—ด์—์„œ ์š”์†Œ๊ฐ€ extracting ๋ ๋•Œ type inference๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
const arr: string[][] = []; //2์ฐจ์›์˜ string ๋ฐฐ์—ด๋กœ annotate

arr.push(100); // error

Interface

interface๋ž€ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„๊ณผ ๊ฐ’ ํƒ€์ž…์„ ๋ฌ˜์‚ฌํ•˜๋Š” ํ•˜๋‚˜์˜ ํƒ€์ž…์ด๋‹ค.

interface Vehicle {
	name: string;
	broken: boolean;
}

const oldCivic = {
	name: 'civic',
	broken:1
}

const printVehicle = (vehicle: Vehicle): void => {
	console.log(`Name : ${vehicle.name}`);
    console.log(`Broken? : ${vehicle.broken}`);
}

printVehicle(oldCivic); // error,
// Vehicle interface์—์„œ broken์„ boolean ํƒ€์ž…์œผ๋กœ annotate ํ–ˆ์œผ๋ฏ€๋กœ 
// broken์ด number ํƒ€์ž…์œผ๋กœ init๋œ oldCivic์€ ํƒ€์ž… ๊ฒ€์‚ฌ์—์„œ ๊ฑธ๋ฆฌ๊ฒŒ ๋จ.

Literal Type and Template Literal Type

Literal Type

๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์ธ string๊ณผ number ํƒ€์ž…์€ ํŠน์ • ๋ฌธ์ž์—ด์ด๋‚˜ ์ˆซ์ž๋ฅผ ํƒ€์ž… ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

let changingString = "Hello World";
changingString = "Ola Mundo";
// let์œผ๋กœ ์„ ์–ธ๋œ changingString์€ ๋‹ค๋ฅธ string๊ฐ’์œผ๋กœ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์œผ๋ฏ€๋กœ string์œผ๋กœ inference
// ^ = let changingString: string

const constantString = "Hello World";
// const๋กœ ์„ ์–ธ๋œ constantString์€ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์œผ๋กœ inference
// ^ = const constantString: "Hello World"

๋‹ค์Œ์€ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…๊ณผ ์œ ๋‹ˆ์–ธ์„ ๊ฒฐํ•ฉํ•ด ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

function textAlignment(s: string, alignment: "left" | "right" | "center") {
  // ...
}
textAlignment("Hello, world", "left");
textAlignment("G'day, mate", "centre"); // *1, error

// ๋‘๋ฒˆ์งธ ์ธ์ž๋Š” "left" ๋˜๋Š” "right" ๋˜๋Š” "center"๋กœ ํƒ€์ž… annotate ๋๊ธฐ ๋•Œ๋ฌธ์— *1์—์„œ error
interface Options {
  height: number;
}
function set(x: Options | "auto") {
  // ...
}
set({ height: 100 });
set("auto");
set("longer"); // error

Literal Inference

๋ฆฌํ„ฐ๋Ÿด inference๋ž€ object๋กœ ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™” ํ• ๋•Œ ํƒ€์ž… ์‹œ์Šคํ…œ์—์„œ ํ•ด๋‹น ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋‚˜์ค‘์— ๊ฐ’์„ ๋ฐ”๊ฟ€์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

const obj = { amount: 0 };
if (someCondition) {
  obj.amount = 1;
}

// ๊ฐ์ฒด ๋‚ด์˜ amount๋ฅผ number ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž… 0์œผ๋กœ annotate์„ ํ–ˆ์ง€๋งŒ ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋‚˜์ค‘์— ๊ฐ’์ด ๋ฐ”๋€Œ์–ด์งˆ ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— number๋กœ inference๋จ

Template Literal Type

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์€ string ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์œ ๋‹ˆ์–ธ์„ ํ†ตํ•ด ๋” ๋งŽ์€ ๋ฌธ์ž์—ด๋กœ ํ™•์žฅํ•œ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ES6์˜ ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€ ๋ฌธ๋ฒ•์„ ๊ฐ€์ง€์ง€๋งŒ, type์˜ ์œ„์น˜์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

type World = "world";

type Greeting = `hi ${World}`;
//   Greeting์˜ ํƒ€์ž…์€ 'hello world'
type Apple = "red" | "sweet";
type Lemon = "Yello" | "sour";

type Fruit = `I_like_${Apple | Lemon}_fruit!`;
//  Fruit์˜ ํƒ€์ž…์€  = "red" | "sweet" | "Yello" | "sour"

"?" and "!" in TypeScript

? : Optional Parameters

ํŒŒ๋ผ๋ฏธํ„ฐ์— ? ์ ‘๋ฏธ์‚ฌ๋ฅผ ์‚ฌ์šฉํ•ด ํŠน์ • ํƒ€์ž…์œผ๋กœ annotation ํ•  ๊ฒฝ์šฐ, ๊ทธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์‹ค์ œ๋กœ ํŠน์ • ํƒ€์ž…๊ณผ undefined ํƒ€์ž…์„ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์‚ฌ์šฉ๋˜์ง€ ์•Š์„๊ฒฝ์šฐ(unused, unspecified) undefined ๊ฐ’์„ ๊ฐ–์ง€๋ฉฐ ๊ฐ’์ด ์žˆ์„์ˆ˜๋„ ์žˆ๊ณ  ์—†์„์ˆ˜๋„ ์žˆ์„๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

function point(x?: number, y?: number){
    if(x) 
        console.log('X : ' + x);
    else 
        console.log('Value of X coordinate not given');
    if(y) 
        console.log('Y : ' + y);
    else 
        console.log('Value of Y coordinate not given');
}
// Function Call: point();
Value of X coordinate not given
Value of Y coordinate not given

// Function Call : point(10);
X : 10
Value of Y coordinate not given

// Function Call : point(10, 20);
X : 10
Y : 20

! : Nullable Types

! ๋ฅผ ์ด์šฉํ•ด annotation๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํƒ€์ž… check์‹œ null๊ณผ undefined๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค.

const v: { p: string | null} = { p: 'text' };

console.log(v.p.toUpperCase()); // Error!

console.log(v.p!.toUpperCase()); // Fine

?? : Nullish Coalescing Operator

??๋Š” || ์—ฐ์‚ฐ์ž์™€ ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์œผ๋กœ null ๋˜๋Š” undefined ๊ฐ’์ธ์ง€ ํ™•์ธํ•˜๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.

์—ฐ์‚ฐ์ž์˜ ์™ผ์ชฝ ๋ณ€์ˆ˜๊ฐ€ null๋˜๋Š” undfined์ผ ๊ฒฝ์šฐ ์—ฐ์‚ฐ์ž ์šฐ์ธก์˜ ๊ฐ’ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const undefinedVar = undefined; 
const value = 'test'; 
const result = undefinedVar ?? value; 

// result === 'test';

!!

!!์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์—ฐ์‚ฐ์ž๋กœ ์ •์˜๋œ ๊ฒƒ์€ ์•„๋‹ˆ๋ฉฐ ๋ณ€์ˆ˜๋ฅผ boolean ํƒ€์ž…์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๊ฐ’์ด ํ• ๋‹น๋˜์–ด ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜, 0, null, undefined์˜ ๊ฐ’์„ ๊ฐ€์งˆ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const array = [1,2];
!!array[0]; // true
!!array[1]; // true
!!array[2]; // false, undefined

const obj = {
  value: 5,
  node: null
};
!!obj.value; // true
!!obj.node;  // false, null

let v = 0;
!!v // false, 0



Reference