๐Ÿ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)
    • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • ์ž๋ฃŒ๊ตฌ์กฐ new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • ๊ฐ์ฒด
    • Arguments
    • higher order function
    • operators-function-control-flow
    • ๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • Object ๊ฐ์ฒด
    • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
    • ์ฐธ์กฐ
    • ํ•จ์ˆ˜
    • ์ƒ์†(Inheritance)
    • this - 'this'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ '์–ด๋–ป๊ฒŒ' ์‹คํ–‰ํ•˜๋А๋ƒ์— ๋”ฐ๋ฅธ 4๊ฐ€์ง€ this ์ •์˜
    • ์ „์—ญ๊ฐ์ฒด(Global object)
    • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - ์ž์—ฐ์ˆ˜, ์ •์ˆ˜, 10์ง„์ˆ˜, 2์ง„์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , ์‹ค์ˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?
    • Hooks๊ณผ useEffect ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
    • Route
    • async wait ์‚ฌ์šฉํ•˜๊ธฐ
    • Hooks API Reference
    • context
    • npm uninstall ํ•˜๋Š”๋ฒ•
    • test ๋งŒ๋“ค๊ธฐ
  • tip
    • ํด๋ฆฐ์ฝ”๋“œ
    • BxSlider๋กœ ํ…์ŠคํŠธ ํ๋ฅด๋Š” ํšจ๊ณผ ๋งŒ๋“ค๊ธฐ
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2๋ถ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • ๋จธ์ง€ ํ›„ branch ์‚ญ์ œํ•˜๊ธฐ
    • ์ง€์˜ฅ์—์„œ ์˜จ Git
    • reset-revert
    • develop branch ๋ฅผ pull ํ•˜๊ณ  ์‹ถ์„๋•Œ
  • conference-and-seminar
    • ๋ชจ๋˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์˜ ์ดํ•ด
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ
    • ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ฒ•
    • ๋ฐ”๋‹๋ผ์ฝ”๋”ฉ ์ˆ˜๊ฐ• ํ›„๊ธฐ
    • async ๊ณผ์ œ ํ›„๊ธฐ - ๋น„๋™๊ธฐ, ๋™๊ธฐ, ํด๋กœ์ €, ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด
    • ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars ์ด์šฉ์ž ์†”๋ฃจ์…˜ ๋ชจ์Œ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • ๊ฐ์ฒด ์ง€๋„
      • ์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • ์ถ”์ƒํ™” ๊ธฐ๋ฒ•
      • 05. ์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • 04. ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • ์›น ๋ธŒ๋ผ์šฐ์ €์— URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์งˆ๊นŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex ํ•ด๋ฒ„๋ ธ์ง€ ๋ญ์•ผ
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd ๋ช…๋ น์–ด ๋ชจ์Œ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN ์„ค์ •ํ•˜๊ธฐ
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans ์˜ค๋‹ต๋…ธํŠธ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 2/2
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 1/2
  • you-dont-know-js
    • ํƒ€์ž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • Core Types
  • number
  • Object Types
  • Array
  • Tuple
  • Enum
  • any
  • Union Types
  • Type Aliases
  • Function Return Types and Void
  • Function Types
  • Function Types and Callbacks
  • The Unknown Type
  • The Never Type

Was this helpful?

  1. typescript

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

const add = (num1, num2) => {
  if(typeof num1 === "number" && typeof num2 === "number") {
    return num1 + num2
  } 
  return Number(num1) + Number(num2);
}

using typescript

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

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

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

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

npm install --save-dev lite-server
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "lite-server"
}

Core Types

  • number

  • String

  • Boolean

  • Object

  • Array

  • Tuple

  • Enum

  • Any

number

function add(n1: number, n2: number) {
  return n1 + n2;
}

const number1 = '5';
const number2 = 2.8;

const result = add(number1, number2);
console.log(result);

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

const person = {
  name: 'sla'
};

console.log(person.nickname);

nickname์„ ์„ค์ •ํ•˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ nickname์„ ํ™•์ธํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๊ฐœ๋ฐœํ™˜๊ฒฝ์—์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

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

6 console.log(person.nickname);

const person: {
  name: string;
} = {
  name: 'sla'
};

console.log(person.name);

Array

const person = {
  name: 'sla',
  hobbies: ['Sports', 'Cooking']
};

for (const hobby of person.hobbies) {
  console.log(hobby.toLocaleUpperCase());
}

console.log(person.name);

toLocaleUpperCase๋ฅผ ์ผ์„๋•Œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.

for (const hobby of person.hobbies) {
  console.log(hobby.map());
}

map์€ Array ๋ฉ”์†Œ๋“œ๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋ฟœ๋ฟœํ•ด์ค€๋‹ค.

Tuple

const person: {
  name: string;
  age: number;
  hobbies: string[];
  role: [number, string];
} = {
  name: 'sla',
  hobbies: ['Sports', 'Cooking'],
  role: [2, 'author']
};

Enum

const ADMIN = 0;

const person = {
  name: 'sla',
  hobbies: ['Sports', 'Cooking'],
  role: ADMIN
};

js์—์„œ ์ƒ์ˆ˜๊ด€๋ฆฌ๋ฅผ ํ•  ๋•Œ๋Š” const์™€ ๋Œ€๋ฌธ์ž ๋ณ€์ˆ˜๋ช…์„ ์ด์šฉํ•˜์—ฌ ํ•˜์˜€๋‹ค.

enum์€ ์—ด๊ฑฐํ˜•์œผ๋กœ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

typescript๋กœ ๋ฐ”๊พผ๋‹ค๋ฉด

enum Role { ADMIN, READ_ONLY, AUTHOR };

const person = {
  name: 'sla',
  hobbies: ['Sports', 'Cooking'],
  role: Role.ADMIN
};

enum Role { ADMIN, READ_ONLY, AUTHOR }; ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด์ฒ˜๋Ÿผ 0, 1, 2์— ํ•ด๋‹น๋œ๋‹ค.

๋ณ€ํ™˜ํ•œ ํŒŒ์ผ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

var Role;
(function (Role) {
    Role[Role["ADMIN"] = 0] = "ADMIN";
    Role[Role["READ_ONLY"] = 1] = "READ_ONLY";
    Role[Role["AUTHOR"] = 2] = "AUTHOR";
})(Role || (Role = {}));
enum Role { ADMIN = 5, READ_ONLY = 'READ_ONLY', AUTHOR };

์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๋„ ๋˜๊ณ  ํ…์ŠคํŠธ๋„ ๋œ๋‹ค.

any

  • ์ตœ๋Œ€ํ•œ ์“ฐ๋Š” ๊ฒƒ์„ ํ”ผํ•˜์ž

  • ๋ช…์‹œ์ ์œผ๋กœ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๊ณ  ์œ ์ถ”๋ฅผ ํ†ตํ•œ๋‹ค๋ฉด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์žฅ์  x

Union Types

function combine(input1: number | string, input2: number | string) {
  let result;
  if (typeof input1 === 'number' && typeof input2 === 'number') {
    result = input1 + input2;
  } else {
    result = input1.toString() + input2.toString();
  }

  return result;
}

const combinedAges = combine(30, 26);

console.log(combinedAges)

| ์„ ์ด์šฉํ•˜์—ฌ ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

Type Aliases

type Combinable = number | string;

function combine(input1: Combinable, input2: Combinable) {
  // ...code
}

type์„ ์ด์šฉํ•˜์—ฌ ์ƒ๋‹จ์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

Function Return Types and Void

function add(n1: number, n2: number): number {
  return n1 + n2
}

parameter ๋งค๊ฐœ๋ณ€์ˆ˜ ์˜†์— : ๋ฅผ ์ด์šฉํ•˜์—ฌ return๊ฐ’์˜ ํƒ€์ž…์„ ์ •์˜ํ•ด์ค€๋‹ค.

function printResult(num: number): void {
  console.log('Result: ' + num);
}

return๊ฐ’์ด ์—†์„ ๋•Œ๋‚˜, return;๋งŒ ํ• ๋•Œ void๋กœ ์ •์˜ํ•œ๋‹ค.

undefined ์ธ ๊ฒฝ์šฐ์€ ์•ˆ๋ ๊นŒ?

function printResult(num: number): undefiend {
  console.log('Result: ' + num);
  return;
}

return; ์ด๋Ÿฐ ์ƒํ™ฉ์„ ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ void ๋กœ ์“ด๋‹ค.

Function Types

function add(n1: number, n2: number): number {
  return n1 + n2
}

let combineValues: (a: number, b: number) => number;

combineValues = add;

console.log(combineValues(8, 8));

๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ํ•จ์ˆ˜๋กœ ์ง€์ •ํ•  ๋•Œ function์ด๋ผ๊ณ  ํ•˜์ง€ ์•Š๋Š”๋‹ค. (a: number, b: number) => number; ์œผ๋กœ ์ง€์ •ํ•œ๋‹ค.

Function Types and Callbacks

function add(n1: number, n2: number, cb: (num: number) => void): number {
  cb(result);
  return n1 + n2
}

console.log(add(1, 2, (num) => console.log(num)));

์ธ์ž์— ๋Œ€ํ•œ ์ •์˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ ํ•œ๋‹ค. ์˜์‹ฌ์ด ๋“ค์–ด ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ addํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•  ๋•Œ ๋งˆ์šฐ์Šค๋ฅผ ์˜ฌ๋ ค๋ณด๋ฉด ํƒ€์ž…์ด ์ง€์ •๋˜์–ด ์žˆ๋‹ค.

The Unknown Type

let userInput: unknown;
let useName: string;

userInput = 5;
userInput = 'sla';

userName = userInput;

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. unknown๋กœ ์„ค์ •ํ•œ๊ฑด string์— ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด any๋Š”?

let userInput: any;
let userName: string;

userInput = 5;
userInput = 'sla';

userName = userInput;

๋ฌธ์ œ ์—†๋‹ค. any๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์ฒ˜๋Ÿผ ์œ ์—ฐํ•˜๊ฒŒ ์“ฐ๊ฒ ๋‹ค๋Š” ๋œป์ด๊ณ  ๊ธฐ๋ณธ์ ์œผ๋กœ ํƒ€์ž…๊ฒ€์‚ฌ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜๊ณ  ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์„ ํฌ๊ธฐํ•˜๊ฒ ๋‹ค ๋Š” ๋œป์ด๋‹ค.

userInput์— ํ• ๋‹นํ•˜๋Š” ๋ฒ•

let userInput: unknown;
let userName: string;

userInput = 5;
userInput = 'sla';

if (typeof userInput === 'string') {
  userName = userInput;
}

extra check๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

The Never Type

function generateError(message: string, code: number): never {
  throw {message: message, errorCode: code};
  // while (true) {}
}

generateError('An error occurred!', 500);

ํ•จ์ˆ˜์—์„œ ์ ˆ๋Œ€ return์ด ์—†์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

generateError์— ๋งˆ์šฐ์Šค๋ฅผ ์˜ฌ๋ฆฌ๋ฉด void๊ฐ€ ๋œจ์ง€๋งŒ ํ›จ์”ฌ ๋ช…ํ™•ํ•˜๋‹ค.

PreviousThe Basic CheatsheetNextType Guards and Differnetiating Types

Last updated 5 years ago

Was this helpful?