TypeScript

  • A javaScript Superset

  • A Language building up on javaScript

  • Adds new Features + Advantages to javascript (μƒˆλ‘œμš΄ 것이 μ•„λ‹ˆλ‹€)

  • Browser can't execute it

  • Compiled to js

const add = (num1, num2) => num1 + num2;

console.log(add('1','2'));

add('1','2') Unwanted Behavior at Runtime

result 23

Mitigation Stategies

Add if check to add fuction Validate & sanitize user input

const button = document.querySelector("button");
const input1 = document.querySelector("num1");
const input2 = document.querySelector("num2");

const add = (num1, num2) => num1 + num2;

button.addEventListener('click', () => consle.log(input1.value, input2.value));

this is not a technical error

it's not an error which is thrown but it's a logical error

the value of an input element is always a number

using typescript

compiler error

μ—¬κΈ°μ„œ !λŠ” μ ˆλŒ€ null이 μ˜€μ§€ μ•ŠλŠ”λ‹€λŠ” λœ»μ΄λ‹€.

νƒ€μž…μŠ€ν¬λ¦½νŠΈλ₯Ό μΆ”κ°€ν•˜λŠ”κ²ƒμ€ typeκ³Ό dataκ°€ 맀우 μ€‘μš”ν•˜λ‹€.

μž₯점

  • clean code

  • 였λ₯˜ λ°©μ§€(예기치 μ•Šμ€ 상황을 ν”Όν•  수 μžˆλ‹€.)

  • μ–΄λ–»κ²Œ μž‘λ™ν•˜λŠ”μ§€ λͺ…μ‹œμ μœΌλ‘œ μ§€μ •

  • μ»΄νŒŒμΌν•˜μ—¬ μ΄μ „λ²„μ „μ˜ λΈŒλΌμš°μ €λ„ μ§€μ›ν•œλ‹€.(Next-gen JavaScript)

  • Non-javaScript Features like Interfaces or Generics

  • Meta-Programming Features like Decorators

  • Rich Configuration Options

  • Modern Tooling that helps even in non-TypeScript Projects

lite server

Core Types

  • number

  • String

  • Boolean

  • Object

  • Array

  • Tuple

  • Enum

  • Any

number

typeScript's type system only helps you during development.

λΈŒλΌμš°μ €λŠ” 지원해주지 μ•ŠλŠ”λ‹€.

JavaScript uses "dynamic types" (resolved at runtime), TypeScript uses "static types" (set during development).

Object Types

nickname을 μ„€μ •ν•˜μ§€ μ•Šμ€ μƒνƒœμ—μ„œ nickname을 ν™•μΈν•˜κ³ μž ν•œλ‹€λ©΄ κ°œλ°œν™˜κ²½μ—μ„œ νƒ€μž…μŠ€ν¬λ¦½νŠΈκ°€ 였λ₯˜λ₯Ό μ•Œλ €μ€€λ‹€.

Property 'nickname' does not exist on type '{ name: string; age: number; }'.

6 console.log(person.nickname);

Array

toLocaleUpperCaseλ₯Ό μΌμ„λ•ŒλŠ” λ¬Έμ œκ°€ μ—†λ‹€.

map은 Array λ©”μ†Œλ“œκΈ° λ•Œλ¬Έμ— νƒ€μž…μŠ€ν¬λ¦½νŠΈκ°€ μ—λŸ¬λ₯Ό λΏœλΏœν•΄μ€€λ‹€.

Tuple

Enum

jsμ—μ„œ μƒμˆ˜κ΄€λ¦¬λ₯Ό ν•  λ•ŒλŠ” const와 λŒ€λ¬Έμž λ³€μˆ˜λͺ…을 μ΄μš©ν•˜μ—¬ ν•˜μ˜€λ‹€.

enum은 μ—΄κ±°ν˜•μœΌλ‘œ μ‰½κ²Œ ν•  수 μžˆλ‹€.

typescript둜 λ°”κΎΌλ‹€λ©΄

enum Role { ADMIN, READ_ONLY, AUTHOR }; μ΄λ ‡κ²Œ μ‚¬μš©ν•  수 μžˆλ‹€. λ°°μ—΄μ²˜λŸΌ 0, 1, 2에 ν•΄λ‹Ήλœλ‹€.

λ³€ν™˜ν•œ νŒŒμΌμ—μ„œ 확인할 수 μžˆλ‹€.

수λ₯Ό λ³€κ²½ν•  수 μžˆλŠ” μˆ«μžλ„ 되고 ν…μŠ€νŠΈλ„ λœλ‹€.

any

  • μ΅œλŒ€ν•œ μ“°λŠ” 것을 ν”Όν•˜μž

  • λͺ…μ‹œμ μœΌλ‘œ νƒ€μž…μ„ λ‚˜νƒ€λ‚΄μ§€ μ•Šκ³  μœ μΆ”λ₯Ό ν†΅ν•œλ‹€λ©΄ νƒ€μž…μŠ€ν¬λ¦½νŠΈ μž₯점 x

Union Types

| 을 μ΄μš©ν•˜μ—¬ νƒ€μž…μ„ μ§€μ •ν•  수 μžˆλ‹€.

Type Aliases

type을 μ΄μš©ν•˜μ—¬ 상단에 μ§€μ •ν•  수 μžˆλ‹€.

Function Return Types and Void

parameter λ§€κ°œλ³€μˆ˜ μ˜†μ— : λ₯Ό μ΄μš©ν•˜μ—¬ returnκ°’μ˜ νƒ€μž…μ„ μ •μ˜ν•΄μ€€λ‹€.

return값이 없을 λ•Œλ‚˜, return;만 ν• λ•Œ void둜 μ •μ˜ν•œλ‹€.

undefined 인 κ²½μš°μ€ μ•ˆλ κΉŒ?

return; 이런 상황을 말할 수 μžˆλ‹€. ν•˜μ§€λ§Œ void 둜 μ“΄λ‹€.

Function Types

λ³€μˆ˜μ˜ νƒ€μž…μ„ ν•¨μˆ˜λ‘œ μ§€μ •ν•  λ•Œ function이라고 ν•˜μ§€ μ•ŠλŠ”λ‹€. (a: number, b: number) => number; 으둜 μ§€μ •ν•œλ‹€.

Function Types and Callbacks

μΈμžμ— λŒ€ν•œ μ •μ˜λŠ” λ§€κ°œλ³€μˆ˜μ—μ„œ ν•œλ‹€. μ˜μ‹¬μ΄ λ“€μ–΄ ν™•μΈν•˜λŠ” 방법은 addν•¨μˆ˜λ₯Ό 호좜 ν•  λ•Œ 마우슀λ₯Ό 올렀보면 νƒ€μž…μ΄ μ§€μ •λ˜μ–΄ μžˆλ‹€.

The Unknown Type

μ΄λ ‡κ²Œ ν•˜λ©΄ λŸ°νƒ€μž„ μ—λŸ¬κ°€ λ‚œλ‹€. unknown둜 μ„€μ •ν•œκ±΄ string에 ν• λ‹Ήν•  수 μ—†λ‹€.

κ·Έλ ‡λ‹€λ©΄ anyλŠ”?

문제 μ—†λ‹€. any둜 μ„€μ •ν•˜λŠ” 것은 μžλ°”μŠ€ν¬λ¦½νŠΈμ²˜λŸΌ μœ μ—°ν•˜κ²Œ μ“°κ² λ‹€λŠ” 뜻이고 기본적으둜 νƒ€μž…κ²€μ‚¬λ₯Ό λΉ„ν™œμ„±ν™”ν•˜κ³  νƒ€μž…μŠ€ν¬λ¦½νŠΈλ₯Ό μ“°λŠ” 것을 ν¬κΈ°ν•˜κ² λ‹€ λŠ” λœ»μ΄λ‹€.

userInput에 ν• λ‹Ήν•˜λŠ” 법

extra checkλ₯Ό μΆ”κ°€ν•œλ‹€.

The Never Type

ν•¨μˆ˜μ—μ„œ μ ˆλŒ€ return이 없을 λ•Œ μ‚¬μš©ν•œλ‹€.

generateError에 마우슀λ₯Ό 올리면 voidκ°€ λœ¨μ§€λ§Œ 훨씬 λͺ…ν™•ν•˜λ‹€.

Last updated