Skip to content

๐Ÿ’Ÿ [2์žฅ] ํƒ€์ž… ์ง‘ํ•ฉ์˜ ๊ฐœ๋…๊ณผ ํƒ€์ž… ํ˜ธํ™˜์„ฑ

hyeyoonS edited this page May 23, 2024 · 6 revisions
const metaData: MetadataProps = {
	author: "ํ˜œ์œค",
	createdAt: 2024-03-24,
	chapter: 2,
	description: "ํƒ€์ž… ์ง‘ํ•ฉ์˜ ๊ฐœ๋…๊ณผ ํƒ€์ž… ํ˜ธํ™˜์„ฑ"
	}
	
const { chapter, description } = metaData;

https://ts.winterlood.com/ ์—ฌ๊ธฐ ์ฐธ๊ณ ํ–ˆ์–ด์š”!

๐Ÿค”ํƒ€์ž… ์ง‘ํ•ฉ์ด ๋ญ์ง€?

** ํƒ€์ž…์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ์ง‘ํ•ฉ์ด๋‹ค.**

Note

์ง‘ํ•ฉ์ด๋ž€? โ‡’ ๋™์ผํ•œ ์†์„ฑ์„ ๊ฐ–๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์€ ๋‹จ์œ„

ex) number ํƒ€์ž…์€ -20, 3, 2, 4, 123, 0.12, โˆž ๋“ฑ์˜ ๊ฐ’์„ ๋ฌถ์–ด ๋†“์€ ์ง‘ํ•ฉ์ด๋‹ค.
๊ทธ๋ ‡๋‹ค๋ฉด ์˜ค์ง ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ํฌํ•จํ•˜๋Š” Number Literal ํƒ€์ž…์€?

let num: 20 = 20;

20 ์ด๋ผ๋Š” Number Literal ํƒ€์ž…์ด ์กด์žฌํ•œ๋‹ค๋ฉด, ์ด ํƒ€์ž…์€ ๋”ฑ ํ•˜๋‚˜์˜ ๊ฐ’(20)๋งŒ์„ ํฌํ•จํ•˜๋Š” ์•„์ฃผ ์ž‘์€ ์ง‘ํ•ฉ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  20์ด๋ผ๋Š” Number Literal ํƒ€์ž…์€, Numberํƒ€์ž…์— ์†ํ•˜๋Š” ๊ฐ’์ด๋‹ค. โ‡’ ๋ชจ๋“  Number Literal ํƒ€์ž…์€ Number์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์ด๋‹ค.

์ด ๋•Œ number ํƒ€์ž…์€ ์Šˆํผํƒ€์ž…(๋ถ€๋ชจํƒ€์ž…)์ด๊ณ , number literal ํƒ€์ž…์€ ์„œ๋ธŒํƒ€์ž…(์ž์‹ํƒ€์ž…)์ด๋‹ค.

1

๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ๊ฐœ๋…์€ ํƒ€์ž… ๊ณ„์ธต๋„์— ์ ์šฉ์ด ๋œ๋‹ค.

2

3

๐Ÿค”ํƒ€์ž… ํ˜ธํ™˜์„ฑ์€ ๋ญ์ง€?

ํƒ€์ž… ํ˜ธํ™˜์„ฑ์ด๋ž€, ์–ด๋– ํ•œ ํƒ€์ž…์„ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์ทจ๊ธ‰ํ•ด๋„ ๊ดœ์ฐฎ์€์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ํƒ€์ž… ํ˜ธํ™˜์„ฑ์€ ํƒ€์ž… ๊ณ„์ธต๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์Šˆํผํƒ€์ž…์˜ ๊ฐ’์„ ์„œ๋ธŒํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰ํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ฐ˜๋Œ€๋กœ, ์„œ๋ธŒํƒ€์ž…์˜ ๊ฐ’์€ ์Šˆํผํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ํ˜ธํ™˜ ๊ฐ€๋Šฅํ•˜๋‹ค.

4

๊ทธ๋ฆฌ๊ณ  ์ด ๋•Œ ์„œ๋ธŒํƒ€์ž… โ‡’ ์Šˆํผํƒ€์ž…์œผ๋กœ ํ˜ธํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์—…์บ์ŠคํŒ…์ด๋ผ๊ณ  ํ•œ๋‹ค.
๋ฐ˜๋Œ€๋กœ ์Šˆํผํƒ€์ž… โ‡’ ์„œ๋ธŒํƒ€์ž…์œผ๋กœ ํ˜ธํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋‹ค์šด์บ์ŠคํŒ…์ด๋ผ๊ณ  ํ•œ๋‹ค.

5

let num1: number = 10;
let num2: 10 = 10;

num1 = num2; // โœ… OK
num2 = num1; // โŒ NO

๐Ÿ€ํƒ€์ž… ๊ณ„์ธต๋„์™€ ํƒ€์ž… ํ˜ธํ™˜์„ฑ ์˜ˆ์‹œ

  • never์€ ๊ณต์ง‘ํ•ฉ์œผ๋กœ, ๋ชจ๋“  ํƒ€์ž…์˜ ์„œ๋ธŒํƒ€์ž…์ด๋‹ค. ๋”ฐ๋ผ์„œ neverํƒ€์ž…์€ ๋ชจ๋“  ํƒ€์ž…์œผ๋กœ ์—…์บ์ŠคํŒ…์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
// never ํƒ€์ž…์€ ๋ชจ๋“  ํƒ€์ž…์˜ ์„œ๋ธŒํƒ€์ž…์ด๋ฏ€๋กœ, ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ์–ด์„œ ์˜๋ฏธ์ƒ์œผ๋กœ๋Š” ์˜ฌ๋ฐ”๋ฅธ ์ฝ”๋“œ์ด๋‹ค.
ํ•˜์ง€๋งŒ never ํƒ€์ž…์ด ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ๊ฐ’์€ undefined์ด๋ฉฐ, ๊ทธ ์™ธ์˜ ๊ฐ’์€ ๊ฐ€์งˆ ์ˆ˜ ์—†๊ธฐ ์—†๋‹ค.
๋”ฐ๋ผ์„œ  ์‹ค์ œ๋กœ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์–ด์„œ ํ• ๋‹น ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚˜ ํƒ€์ž… ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š”์ง€ ์‹ค์ œ๋กœ ํ™•์ธ์ด ์–ด๋ ต๋‹ค.

let neverVar: never;

let a: number = neverVar;            // never -> number
let b: string = neverVar;            // never -> string
let c: boolean = neverVar;           // never -> boolean
let d: null = neverVar;              // never -> null
let e: undefined = neverVar;         // never -> undefined
let f: [] = neverVar;                // never -> Array
let g: {} = neverVar;                // never -> Object
// ํ•จ์ˆ˜ ์•ˆ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฌดํ•œ๋ฃจํ”„๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋ฉด never ํƒ€์ž…์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
const neverFunc = (): never => {
    while(true){}
}

let num:number = neverFunc(); // ์—…์บ์ŠคํŒ…
let str:string = neverFunc();
let bool:boolean = neverFunc()
  • ๋ฐ˜๋Œ€๋กœ, ์–ด๋–ค ํƒ€์ž…๋„ neverํƒ€์ž…์œผ๋กœ ๋‹ค์šด์บ์ŠคํŒ… ํ•  ์ˆ˜ ์—†๋‹ค.
let a: never = 1;                 // number -> never โŒ
let b: never = "hello";           // string -> never โŒ
let c: never = true;              // boolean -> never โŒ
let d: never = null;              // null -> never โŒ
let e: never = undefined;         // undefined -> never โŒ
let f: never = [];                // Array -> never โŒ
let g: never = {};                // Object -> never โŒ
  • unknown์€ ๋ชจ๋“  ํƒ€์ž…์„ ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ๊ฐ–๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ „์ฒด ์ง‘ํ•ฉ์ด๋‹ค. ๋ชจ๋“  ํƒ€์ž…์€ unknown์œผ๋กœ ์—…์บ์ŠคํŒ… ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜, ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ํ•˜๋ฉฐ ํƒ€์ž… ํ˜ธํ™˜์„ฑ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ํƒ€์ž… ๋‹จ์–ธ์„ ์‚ฌ์šฉํ•˜๋‹ค ์ผ์–ด๋‚ฌ๋˜ ์˜ค๋ฅ˜๋ฅผ ์งง๊ฒŒ๋‚˜๋งˆ ๊ณต์œ ํ•ด๋ด…๋‹ˆ๋‹ค.

๐ŸŽฏํƒ€์ž… ๋‹จ์–ธ์„ ์‚ฌ์šฉํ•˜๋‹ค ์ผ์–ด๋‚œ ์˜ค๋ฅ˜

const def = 'string' as number;  // ์˜ค๋ฅ˜ ๋ฐœ์ƒ - Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other.
  • ํƒ€์ž… ๋‹จ์–ธ์—๋„ ์กฐ๊ฑด์ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • A as B๋กœ ํ‘œํ˜„ํ–ˆ์„ ๋•Œ A๊ฐ€ B์˜ ์Šˆํผํƒ€์ž…์ด๊ฑฐ๋‚˜ B๊ฐ€ A์˜ ์Šˆํผํƒ€์ž…์ด์–ด์•ผ๋งŒ ํƒ€์ž… ๋‹จ์–ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๐ŸŽฏ์šฐํšŒ ๋ฐฉ๋ฒ•

๋‹ค์ค‘ ๋‹จ์–ธ

const def = 'string' as unknown as number;
  • ๊ถŒ์žฅ๋˜์ง€๋Š” ์•Š๋Š” ์‚ฌ์šฉ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•˜๋„ค์š” ๐Ÿ˜ญ๐Ÿ˜ญ

Clone this wiki locally