๐Ÿคฏ Javascript์™€ Typescript์— ๋Œ€ํ•ด ์•Œ์•„๋‘๋ฉด ์ข‹์€ 3๊ฐ€์ง€ ์ด์ƒํ•œ ์ , 1๋ถ€

์ ญ ๐Ÿค–



ํ˜„์žฌ ์ €๋Š” ์นœ๊ตฌ๋“ค๊ณผ ์ด ๋†€๋ผ์šด ๋„๊ตฌ์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐ๋ฅผ ๋‚˜๋ˆ„๋‹ค๊ฐ€ Javascript์™€ ๊ทธ์˜ ๋น„๋ฐ€ ๐Ÿฅท์„ ์กฐ๊ธˆ ๋” ์ž˜ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ๊ฒƒ์„ ์šฐ๋ฆฌ๊ฐ€ ์•Œ์ง€ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๊ทธ ์ค‘ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค.



๋ฐฐ์—ด์€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค ๐Ÿฅฒ:




# if you are trying to check if an array has the type of 'Array'
# my friend, you are doing wrong

console.log(typeof "something") # "string"
console.log(typeof {}) # "object"
console.log(typeof []) # "object"


์™œ ์ด๋Ÿฐ ์ผ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๊นŒ? ๋‚ด๊ฐ€ ๋ฏธ์ณค์–ด? ์•„๋‹ˆ์š”, ๋‹น์‹ ์€ ์•„๋‹ ์ˆ˜๋„ ์žˆ๊ณ  ์•„๋‹ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด๊ฒƒ์ด ์š”์ ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ผ์ด ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ๊ฐ€ ๋ชฉ๋ก ๋˜๋Š” ๋ฐฐ์—ด์ด์ง€๋งŒ ๋ชจ๋“  ๋…ผ๋ฆฌ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด์œ ํ•˜๋Š” ๊ฐœ์ฒด๊ฐ€ ๊ฐœ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ฐœ์ฒดx์—์„œ ์†์„ฑobj์— ์•ก์„ธ์Šคํ•˜๋ ค๊ณ  ํ•œ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์‹ญ์‹œ์˜ค.

const obj = { x: 'Hello' }

console.log(obj.x) # 'Hello'


์ด์ œ ๋ฐฐ์—ด์—์„œ length๋ผ๋Š” ์†์„ฑ์— ์•ก์„ธ์Šคํ•˜๋ ค๊ณ  ํ•œ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์‹ญ์‹œ์˜ค.

# This is what is happening behind the scenes

const arr = [2]
console.log(arr.length) # 1


๊ฐœ์ฒด ์•ก์„ธ์Šค์™€ ์†์„ฑ ๊ธธ์ด์˜ ์ฐจ์ด์ ์„ ๋ณด์…จ์Šต๋‹ˆ๊นŒ? ์ •ํ™•ํžˆ ์ด๊ฒƒ์ด ๋ฐฐ์—ด์„ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด object ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•œ ์ด์œ ์ž…๋‹ˆ๋‹ค.

any์™€ unknown ๐Ÿฅธ์˜ ์ฐจ์ด์ :


any ์™€ ์œ ์‚ฌํ•˜๊ฒŒ any ๊ฐ’์„ unknown ์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ any ์™€ ๋‹ฌ๋ฆฌ unknown ์œ ํ˜•์˜ ๊ฐ’์— ๋Œ€ํ•œ ์†์„ฑ์— ์•ก์„ธ์Šคํ•˜๊ฑฐ๋‚˜ ์†์„ฑ์„ ํ˜ธ์ถœ/์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ unknown ๋˜๋Š” any๋งŒ ์‚ฌ์šฉํ•˜์—ฌ unknown ์œ ํ˜•์˜ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

type MyCoolProps {
   newOnboardingFlow: any;
}

...

try{...}
catch(error: unknown){...}


์‹œ๊ฐ„์ด 0์ธ ๊ฒฝ์šฐ์—๋„ setTimeout์ด ์ง€์—ฐ๋ฉ๋‹ˆ๋‹ค.



๊ธ€๋กœ๋ฒŒsetTimeout() ๋ฉ”์„œ๋“œ๋Š” ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋˜๋ฉด ํ•จ์ˆ˜ ๋˜๋Š” ์ง€์ •๋œ ์ฝ”๋“œ ์กฐ๊ฐ์„ ์‹คํ–‰ํ•˜๋Š” ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. x๋ฐ€๋ฆฌ์ดˆ ํ›„์— ๋ Œ๋”๋งํ•˜๋Š” ์ •๋ง ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

setTimeout(() => console.log('Hello dev.to', 1000))

# After 1 second
# Hello dev.to


๊ทธ๋Ÿฌ๋‚˜ ๋ฐ€๋ฆฌ์ดˆ๋ฅผ zero๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ ์ด ์ •์˜๋Š” ์‚ฌ์‹ค์ด ์•„๋‹™๋‹ˆ๋‹ค.

setTimeout(() => console.log('Hello dev.to', 0))
console.log("Bye")

# outputs:
# Bye
# Hello dev.to


์ด ์ด์ƒํ•œ ๋งˆ๋ฒ•์ด ์™œ ์ผ์–ด๋‚˜๋Š”์ง€ ๋ฌผ์–ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๊ทธ ์ด์œ ๋Š” ํƒ€์ด๋จธ๋ฅผ 0์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋‹ค์Œ ๋ฐ˜๋ณต์—์„œ setTimeout๋ฅผ ์‹คํ–‰ํ•˜๋ฏ€๋กœ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ๋‘ ๋ฒˆ์งธ console.log๋ฅผ ์‹คํ–‰ํ•œ ํ›„ ์Šคํƒ์„ ํ™•์ธํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

๋‹ค์Œ ๋ฐ˜๋ณต์€ ๋‹ค์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle.
๐Ÿ’ฐ ์˜ค๋Š˜์˜ 2์„ผํŠธ์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‹๋‚ด ์ƒ๊ฐ์„ ๊ณต์œ ํ•˜๋Š” ๊ณณ์—์„œ ํŒ”๋กœ์›Œ 500๋ช…์„ ๋‹ฌ์„ฑํ•˜๋„๋ก ๋„์™€์ฃผ์„ธ์š”.

๐Ÿ‘‡ ๋Œ“๊ธ€ ์„น์…˜์—์„œ ์ด ์ฃผ์ œ์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ์„ธ์š”.

๐Ÿ‘‰ ๊ทธ๊ฒƒ์— ๋Œ€ํ•ด ๋” ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ €๋ฅผ ํŒ”๋กœ์šฐํ•˜์„ธ์š”.

โœ… ์ œ medium๊ณผ my personal website ์ฃผ์ œ์˜ ๋ชจ๋“  ์Šคํƒ€ํŠธ์—…, ๊ธฐ์ˆ  ๋ฐ ๊ธฐ์—…๊ฐ€ ์ •์‹ ์„ ์“ฐ๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ