๋ฐ˜์‘ํ˜•

Programming/TypeScript 7

[TypeScript] ์ œ๋„ค๋ฆญ ์ปดํฌ๋„ŒํŠธ (generic component) ์ •์˜ ๋ฐ ์‚ฌ์šฉ๋ฐฉ๋ฒ•

์ด ๊ธ€์€ ์›๊ธ€์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. Definition TypeScript Generics๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. generics๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์žฅ๊ธฐ์ ์œผ๋กœ ํ™•์žฅ๊ฐ€๋Šฅํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์œ ์—ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณด์žฅํ•ด์ค๋‹ˆ๋‹ค. Example # Props interface Props { renderItem: (item: T) => React.ReactNode; keyExtractor: (item: T) => string; data: T[]; } data: ์ œ๋„ค๋ฆญ ํƒ€์ž… T๋ฅผ ๋ฐ›์„ ๋ฐฐ์—ด ์š”์†Œ renderItem: ํƒ€์ž… T์˜ react ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ keyExtractor: ํ‚ค๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜ # Component ์ •์˜ const GenericList = ({ data,..

[Understanding TypeScript] Section 1. ์‹œ์ž‘ํ•˜๊ธฐ

์ด ๊ธ€์€ udemy์˜ Typescript :๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‹ค์ „ํ˜• ํ”„๋กœ์ ํŠธ๊นŒ์ง€ with React + NodeJS ๊ฐ•์ขŒ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค 1. ํ™˜์˜ pass 2. TypeScript๋ž€ ๋ฌด์—‡์ด๋ฉฐ ์™œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ superset ์ž…๋‹ˆ๋‹ค. ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง„ ์–ธ์–ด๊ฐ€ ์•„๋‹Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•ด ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ๊ณผ ์žฅ์ ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ JS๋กœ ์ปดํŒŒ์ผํ•˜๋Š” ๊ฐ•๋ ฅํ•œ ์ปดํŒŒ์ผ๋Ÿฌ์ž…๋‹ˆ๋‹ค. ํƒ€์ž…์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ € ๋Ÿฐํƒ€์ž„์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์ „์— ์ฝ”๋“œ์˜ ์—๋Ÿฌ๋ฅผ ๋ฏธ๋ฆฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ ์ค‘ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋กœ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋ฅผ ์ดˆ๊ธฐ์— ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ ) ๋ธŒ๋ผ์šฐ์ €์™€ ๊ฐ™์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. node.js๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. 3..

[TypeScript] Error: Redundant double negation. no-extra-boolean-cast ์˜ค๋ฅ˜ ์›์ธ ๋ฐ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ ์ค‘ ๋‹ค์Œ์˜ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. Error: Redundant double negation. no-extra-boolean-cast {text} Why ์œ„ ์—๋Ÿฌ๋Š” ESLint์˜ no-extra-boolean-cast ๊ทœ์น™์œผ๋กœ, ๋ถˆํ•„์š”ํ•œ ๋ถˆ๋ฆฌ์–ธ ์บ์ŠคํŠธ(๋ถˆ๋ฆฐ ๊ฐ’์„ ๊ฐ•์ œ๋กœ ์บ์ŠคํŒ…ํ•˜๋Š” ๊ฒƒ)๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๊ทœ์น™์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ !!visible์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋ถˆ๋ฆฐ ๊ฐ’์„ ๊ฐ•์ œ๋กœ ์บ์ŠคํŒ…ํ•˜๊ณ  ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์ค‘ ๋ถ€์ • ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์ด ๋  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. How to solve ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ด์ค‘ ๋ถ€์ • ์—ฐ์‚ฐ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ESLint ์—๋Ÿฌ๊ฐ€ ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค. visible ์ž์ฒด๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. {text}

[TypeScript] Could not find a declaration file for module ์˜ค๋ฅ˜ ์›์ธ ๋ฐ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ ์ค‘ ๋‹ค์Œ์˜ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. Could not find a declaration file for module Why? ์ด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋Š” ์—ฐ๊ฒฐ๋œ declaration file ์ด ์—†๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์•ˆ์— ๋ชจ๋“ˆ์ด๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ „ํ˜•์ ์ธ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค. declaration file(.d.ts)๋Š” ๋ชจ๋“ˆ์ด๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ types, functions ๋ฐ ๊ธฐํƒ€ ์†์„ฑ์„ ์„ค๋ช…ํ•˜๊ณ  ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ฝ”๋“œ์—์„œ ์ด๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„ค์น˜๋œ ํŠน์ • ๋ชจ๋“ˆ/๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ type ์ •์˜๊ฐ€ ์—†์–ด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ ์ดํ•ดํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋‹ค์Œ์˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. How to solve the problem ์ด ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”..

[TypeScript] cannot read properties of undefined ์—๋Ÿฌ ์›์ธ ๋ฐ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋˜ ๋„์ค‘ ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. Why? ํ•ด๋‹น properties์˜ ๊ฐ’์„ ์ฝ์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค. How to solve the problem ํ•ด๋‹น ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€์ธ๋ฐ์š”, ์ €๋Š” ๊ทธ ์ค‘์—์„œ๋„ Optional Chaining ์—ฐ์‚ฐ์ž (๋ฌผ์Œํ‘œ ์—ฐ์‚ฐ์ž)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ์‚ฌ์šฉ ์˜ˆ์ œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. return ( {item?.name} ) ์ด๋ ‡๊ฒŒ Optional Chaining ์—ฐ์‚ฐ์ž (๋ฌผ์Œํ‘œ ์—ฐ์‚ฐ์ž)๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์œ ํšจํ•œ ๊ฐ’์ด ์—†์„ ๋•Œ๋Š” undefined๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ฐ’์ด ์žˆ์„ ๋•Œ๋Š” Optional Chaining ์—ฐ์‚ฐ์ž ์ดํ›„ ๋‚ด์šฉ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด๋‹น ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[TypeScript] The left-hand side of an assignment expression may not be an optional property access ์˜ค๋ฅ˜ ์›์ธ ๋ฐ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. TypeScript ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ ref๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ scrollTop ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋˜ ๋„์ค‘ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค someRef?.current?.scrollTop = 0; TS2779: The left-hand side of an assignment expression may not be an optional property access Why? ์ŠคํŽ™ ์ƒ์—์„œ ์˜ต์…”๋„ ์ฒด์ด๋‹๊ณผ ํ• ๋‹น์„ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. The following is not supported, although it has some use cases; see Issue #18 for discussion: optional property assignment: a?.b = c How to solv..

[TypeScript] Property 'offsetTop' does not exist on type 'Element'.ts(2339) ์˜ค๋ฅ˜

Intro ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋˜ ๋„์ค‘ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. Property 'offsetTop' does not exist on type 'Element'.ts(2339) Why? document.querySelector('.page').offsetTop ๋‹ค์Œ์˜ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ๋งž๋Š” ์ฝ”๋“œ์ด์ง€๋งŒ, ๊ฒฐ๊ณผ๊ฐ’์ด null์ด ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ null ์ฒดํฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‘๋ฒˆ์งธ๋กœ๋Š”, ํ˜„์žฌ ๋ฆฌํ„ด ํƒ€์ž…์€ Element์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Element๋Š” offsetTop์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. HtmlElement๋กœ ๋ฐ˜ํ™˜๋  ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ•  ์ˆœ ์žˆ์ง€๋งŒ, SVGElement๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ๋Š” ์ด ์ •๋ณด๊ฐ€ ์ •ํ™•ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๋ถ€๋ถ„ ์—ญ์‹œ type-a..

๋ฐ˜์‘ํ˜•