๐Ÿคทโ€โ™€๏ธ ๊ฐœ๋…๊ณต๋ถ€: 3. Typescript๊ฐ€ ๋ญ˜๊นŒ? (1) ( basic type, Inference, assertion, guard)

Ko Seoyoungยท2021๋…„ 3์›” 9์ผ
0

Typescript๋ž€?

What is TypeScript?
ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ์ •์  ํƒ€์ž… ์ •์˜(static type definitions)๋ฅผ ๋”ํ•ด ๊ตฌ์ถ•๋œ ์˜คํ”ˆ์†Œ์Šค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋‹ค.
(Microsoft์—์„œ ๊ฐœ๋ฐœํ•˜๊ณ  ๊ด€๋ฆฌํ•จ)

์ •์ ํƒ€์ž…์–ธ์–ด๋ž€?


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

  • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋Œ์•„๊ฐ€๋Š” ์–ด๋””์„œ๋“  ๋Œ์•„๊ฐ„๋‹ค. (In a browser, on Node.JS or in your apps.)

  • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์œผ๋กœ์„œ ECMA์˜ ์ตœ์‹  ํ‘œ์ค€์„ ์ถฉ๋ถ„ํžˆ ์ง€์›ํ•œ๋‹ค. ํƒ€์ž…์ด๋ผ๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ES7์ดํ•˜์˜ ํ‘œ์ค€์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ES5๋ฅผ ํฌํ•จํ•˜๋Š” ์ง‘ํ•ฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์˜ ES5 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ES6์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ๋“ค์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Babel๊ณผ ๊ฐ™์€ ๋ณ„๋„ ํŠธ๋žœ์ŠคํŒŒ์ผ๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ES6์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ๊ธฐ์กด์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Typescript์˜ ์žฅ์ 

์ •์  ํƒ€์ž… ์–ธ์–ด(static type language)์ด๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์‹œ ์‹œ๊ฐ„์ด ์กฐ๊ธˆ ๊ฑธ๋ฆฌ๋”๋ผ๋„ ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฒฝ์šฐ๋Š” ๋™์  ํƒ€์ž… ์–ธ์–ด(dynamic type language)์ด๊ธฐ ๋•Œ๋ฌธ์— ๋Ÿฐํƒ€์ž„ ์†๋„๋Š” ๋น ๋ฅด์ง€๋งŒ ํƒ€์ž… ์•ˆ์ •์„ฑ์ด ๋ณด์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งŒ๋“ค์–ด ์กŒ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ MS์—์„œ๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์— โ€œJavaScript that scales(ํ™•์žฅ๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ)โ€๋ผ๋Š” ์Šฌ๋กœ๊ฑด์„ ๋งŒ๋“ค์—ˆ๋‹ค.


Basic Type

Basic type์—๋Š” ์šฐ๋ฆฌ๊ฐ€ javascript์—์„œ ํ”ํžˆ ์‚ฌ์šฉํ•˜๊ธธ ์˜ˆ์ƒํ•˜๋Š” ๋‹จ์ˆœํ•œ ๋‹จ์œ„์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค.

Boolean(boolean), Number(number), String(string), Array(number[], Array)
๐Ÿ‘‰ ์–˜๋„ค๋“ค์€ ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ pass!

  • Tuple: ๊ณ ์ •๋œ ๊ฐœ์ˆ˜์™€ ํƒ€์ž…์„ ๊ฐ–๋Š” ๋ฐฐ์—ด์„ ํ‘œํ˜„ํ•  ์ˆ˜์žˆ๋‹ค.
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
  • Enum: ์—ฐ๊ด€๋œ ์ƒ์ˆ˜ ์ง‘ํ•ฉ์— ์นœ์ˆ™ํ•œ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•œ๋‹ค(์—ด๊ฑฐํ˜• ํƒ€์ž…)
enum Color {
  Red,
  Green,
  Blue,
}
let c: Color = Color.Green;
  • Unknown: ๋ชจ๋ฅด๋Š” ํƒ€์ž… (unknown์ธ ๊ฒฝ์šฐ typeof๋กœ ์ฒดํฌ๋ฅผ ํ•ด์ค˜์•ผํ•จ)

if (typeof maybe === "string"){}

  • Any: ์•„๋ฌดํƒ€์ž…์ด๋‚˜ ๊ฐ€๋Šฅ
const temp:any = 4
temp.toString()

any์— ์ˆซ์ž๋ฅผ ํ• ๋‹นํ•˜๊ณ  ์ œ๋ฉ‹๋Œ€๋กœ ๋ฌธ์ž์—ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด๋„ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์žก์•„์ฃผ์ง€ ์•Š์œผ๋‹ˆ ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

  • Void: any์˜ ๋ฐ˜๋Œ€๋ผ๊ณ ๋„ ๋ณผ ์ˆ˜ ์žˆ์Œ, ์•„๋ฌด๊ฒƒ๋„ ํƒ€์ž…์ด ์—†์„ ๋•Œ void ์”€ (์ฃผ๋กœ ํ•จ์ˆ˜๋ฆฌํ„ดํƒ€์ž…)

  • Null, Undefined ํƒ€์ž…๋„ ์žˆ๋Š”๋ฐ ๋ณ„๋กœ ์•ˆ ์œ ์šฉํ•จ

  • Never: ์—๋Ÿฌ๋ฅผ ๋ฆฌํ„ดํ•˜๊ฑฐ๋‚˜, ๋ฌดํ•œ๋ฃจํ”„์—ฌ์„œ ์•„์˜ˆ ๋ฆฌํ„ดํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์˜ ํ•จ์ˆ˜ ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ ์“ฐ์ž„.

  • Object: non-primitive type์œผ๋กœ number, string, boolean, bigint, symbol, null, or undefined๊ฐ€ ์•„๋‹˜! โ†’ ์ผ๋ฐ˜์ ์œผ๋กœ ๋”ฑํžˆ ์•ˆ ํ•„์š”ํ•จ


Type Inference(ํƒ€์ž… ์ถ”๋ก )

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ ๋ช…๋ฐฑํ•œ ํƒ€์ž…์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ํƒ€์ž…์„ ์ถ”๋ก ํ•œ๋‹ค.

let x = 3;
//  ^ = let x: number

let x = [0, 1, null];
//  ^ = let x: (number | null)[]

Type assertion

๋ณ€์ˆ˜์˜ ํ˜„์žฌ ํƒ€์ž…๋ณด๋‹ค ๋‚ด๊ฐ€ ์•Œ๊ณ ์žˆ๋Š” ์ด ํƒ€์ž…์ด ๋” ๋ช…ํ™•ํ•  ๋•Œ type assertion์„ ํ•œ๋‹ค. (assert๊ฐ€ ์ฃผ์žฅํ•˜๋‹ค๋ผ๋Š” ๋œป)

Type assertions are a way to tell the compiler โ€œtrust me, I know what Iโ€™m doing.โ€ (์•ผ์•ผ ๋‚˜ ์ด๊ฑฐ ๋ฌด์Šจํƒ€์ž…์ธ์ง€ ์•Œ์•„. ๋‚˜ ๋ฏฟ์–ด๋ด.)

as-syntax์™€ angle-bracket ๋‘ ๊ฐ€์ง€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

// '๋‚˜ ์ด๊ฑฐ ํ™•์‹คํžˆ ๋ฌธ์ž์—ด์ธ๊ฑฐ ์•Œ์•„'ํ•˜๋ฉด์„œ as ๋กœ string์ด๋ผ๊ณ  ํƒ€์ž… ์ง€์ •ํ•˜์—ฌ length๋ฅผ ์‚ฌ์šฉ
let someValue: unknown = "this is a string";
// as-syntax
let strLength: number = (someValue as string).length;
// angle-bracket
let strLength: number = (<string>someValue).length;

JSX์™€ ๊ฐ™์ด ์“ฐ์ผ๋•Œ๋Š” as-style assertion๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.

(โ—๏ธ์ถ”๊ฐ€์ ์œผ๋กœ Number, String, Boolean, Symbol, or Object ์ด๋Ÿฐ๊ฒƒ๋“ค์€ number, string, boolean ๋“ฑ๊ณผ ๊ฐ™์ด ํƒ€์ž…์œผ๋กœ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„๋ณด์ด๋Š”๋ฐ ์Ÿค๋„ค๋Š” ํƒ€์ž… ํ• ๋‹นํ•  ๋•Œ ์“ฐ์ง€ ์•Š๋Š”๋‹ค)


Type guard

ํƒ€์ž…์„ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ช…์‹œ์ ์œผ๋กœ ํƒ€์ž…์„ ๊ฒ€์‚ฌํ•˜์ž!

Type guard๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์กฐ๊ฑด๋ฌธ์—์„œ ๊ฐ์ฒด์˜ ํƒ€์ž…์„ ์ขํ˜€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

์ด ๋งํฌ๊ฐ€ ์ •๋ฆฌ๊ฐ€ ๊น”๋”ํ•ด์„œ ์ด๊ฑฐ ์ฝ์œผ๋ฉด ๋œ๋‹ค.

(โ—๏ธ ์˜ฌํ•ด ๋ฒ„๋ ค์•ผ ํ•  ts ๋‚˜์œ ๋ฒ„๋ฆ‡ 10 ๊ฐ€์ง€ 4๋ฒˆ์งธ์— ๋”ฐ๋ฅด๋ฉด Type assertion๋ณด๋‹ค type guard๋ฅผ ํ†ตํ•ด ๋ช…์‹œ์ ์œผ๋กœ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ์ด ์ถ”ํ›„ ์œ ์ง€๋ณด์ˆ˜ ๋น„์šฉ์„ ๋” ๋‚ฎ์ถ˜๋‹ค๊ณ  ํ•œ๋‹ค!)

type guard๋ฅผ ์œ„ํ•œ js operator

  • typeof

    ์‚ฌ์šฉ๋ฒ•: typeof operand

    operand์˜ ํƒ€์ž…์„ string์œผ๋กœ ๋ฐ˜ํ™˜ (ex) type of "blabla" === "string" โ†’ true)

  • instanceof

    ์‚ฌ์šฉ๋ฒ•: object instanceof constructor

    object๊ฐ€ constructor์˜ ์ธ์Šคํ„ด์Šค์ด๋ฉด true ๋ฐ˜ํ™˜

  • in

    ์‚ฌ์šฉ๋ฒ•: prop in object

    prop์ด object์˜ property๋ฉด true ๋ฐ˜ํ™˜


์ฐธ๊ณ ์ž๋ฃŒ

TypeScript #1 ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

Typescript ๊ณต์‹๋ฌธ์„œ - Type Inference

profile
Web Frontend Developer ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป #React #Nextjs #ApolloClient

0๊ฐœ์˜ ๋Œ“๊ธ€