๊ธฐ์กด ๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ์— TypeScript ์ ์šฉํ•˜๊ธฐ
728x90

Today, What I learned?

๊ธฐ์กด ํ”„๋กœ์ ํŠธ์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋ฆฌํŒฉํ† ๋ง์„ ์‹œ๋„ํ•˜๊ธฐ ์•ž์„œ, ํ˜ผ์ž ํ–ˆ๋˜ ํˆฌ๋‘๋ฆฌ์ŠคํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

๋จผ์ € ๊ธฐ์กด ํ”„๋กœ์ ํŠธ๋Š” ๋ฆฌ๋•์Šค์™€ ๋ผ์šฐํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ํŽ˜์ด์ง€ ์ด๋™๊ณผ CRUD๊ฐ€ ์ ์šฉ๋˜์–ด ์žˆ๋Š” ํˆฌ๋‘๋ฆฌ์ŠคํŠธ์˜€๋‹ค.

๊ธฐ๋ณธ ๋ฆฌ๋•์Šค๊ฐ€ ์ ์šฉ๋˜์–ด ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋จผ์ € redux-toolkit์œผ๋กœ ๋ฆฌํŒฉํ† ๋ง ํ•ด์ค€ ํ›„ ๋ณธ๊ฒฉ์ ์ธ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ ์šฉ ์‹œ์ž‘!..

ํŒจํ‚ค์ง€ ์„ค์น˜ํ•˜๊ธฐ

๋จผ์ € ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์™€ ๊ด€๋ จ๋œ ํŒจํ‚ค์ง€๋“ค์„ ์„ค์น˜ํ•ด ์ฃผ์—ˆ๋‹ค.

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๊ณต์‹๋ฌธ์„œ์—์„œ ๋ณด๊ณ  ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ ํ›„์—,

https://www.typescriptlang.org/download

 

How to set up TypeScript

Add TypeScript to your project, or install TypeScript globally

www.typescriptlang.org


@types ๊ด€๋ จ ํŒจํ‚ค์ง€๋“ค์„ ์„ค์น˜ํ•ด์ฃผ์—ˆ๋Š”๋ฐ ์ด ํ”„๋กœ์ ํŠธ๋Š”  yarn ํŒจํ‚ค์ง€๋กœ ๋งŒ๋“  ํ”„๋กœ์ ํŠธ๋ผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋‹ค.

$ yarn add typescript @types/node @types/react @types/react-dom @types/jest --dev

tsconfig.json ํŒŒ์ผ ์„ค์ •ํ•˜๊ธฐ

init์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•œ ํŒŒ์ผ์—์„œ

$ tsc --init

๋‘ ๊ฐ€์ง€ ์˜ต์…˜ ์ •๋„๋งŒ ์ฃผ์„์„ ํ•ด์ œ์‹œ์ผฐ๋‹ค.

  • noImplicitAny : any๋ผ๋Š” ํƒ€์ž…์ด ์˜๋„์น˜ ์•Š๊ฒŒ ๋ฐœ์ƒํ•  ๋•Œ ์—๋Ÿฌ ๋„์›€
  • strictNullChecks : null, undefined ๋“ฑ ์กฐ์ž‘ํ•˜๋ฉด ์—๋Ÿฌ ๋„์›€

 

ํŒŒ์ผ ์ˆ˜์ •

.js๋‚˜ .jsx ํŒŒ์ผ์„ .ts , .tsx ํŒŒ์ผ๋กœ ํ™•์žฅ์ž๋ฅผ ๋ฐ”๊ฟ”์ฃผ๊ณ  ํƒ€์ž…๋“ค์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ...

๋ณธ๊ฒฉ์ ์ธ ์—๋Ÿฌ์˜ ์‹œ์ž‘..

 

 

๐Ÿšง TypeScript์—์„œ styled-components ์ ์šฉ

์ฒ˜์Œ๋ถ€ํ„ฐ ๋‚œํ•ญ ๋ฐœ์ƒ!....

ts๋กœ ํŒŒ์ผ์„ ๋ณ€ํ™˜ํ•˜๋‹ˆ styled-componets import ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”๋ฐ ๊ด€๋ จ ํŒจํ‚ค์ง€๋ฅผ ์ถ”๊ฐ€ ์„ค์น˜ํ•ด ์ฃผ๋‹ˆ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค.

yarn add --dev @types/styled-components

์ด๋Ÿฐ ์‹์œผ๋กœ ํŒจํ‚ค์ง€๋ฅผ ๋ช‡ ๊ฐœ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•˜๋Š” ์ผ์ด ์žˆ์—ˆ๋‹ค.

 

 

๐Ÿšง tsconfig์— "jsx" ์„ค์ • ์ถ”๊ฐ€

'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.

์ตœ์ƒ์œ„ ๋‹จ์— react๋ฅผ import ํ•ด์ฃผ๋ฉด ๋˜๋Š” ์—๋Ÿฌ๋ผ๋Š”๋ฐ, tsconfig์— jsx ์„ค์ •๋งŒ ๋„ฃ์–ด์ค˜๋„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

Are you rendering any jsx in the file? If so, you do need to import React, since those jsx tags compile into calls to React.createElement(). Without the import, the references to React are trying to reference a global variable, which then results in that error.

If you're using eslint, i'd recommend using eslint-plugin-react and turning on the rule react-in-jsx-scope, which will catch this case.

 

jsx์—์„œ ๋ Œ๋”๋ง ํ•  ๋•Œ React.createElement() ํ˜ธ์ถœ์ด ํ•„์š”ํ•œ๋ฐ, import๊ฐ€ ์—†๋‹ค๋ฉด ๋ฆฌ์•กํŠธ๋Š” ์ „์—ญ๋ณ€์ˆ˜์—์„œ ์ด ํ˜ธ์ถœ์„ ํ•˜๋ ค๊ณ  ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๋‚ด์šฉ ๊ฐ™๋‹ค.

์ด ์™ธ์—๋„ ๋งŽ์€ ์—๋Ÿฌ๊ฐ€ ๋‚  ๋ฐ˜๊ธฐ๊ณ  ์žˆ์ง€๋งŒ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€ ๋ณด๋„๋ก ํ•˜์ž ๐Ÿ˜‡..

 

 

์ฐธ๊ณ ๊ธ€

https://itecnote.com/tecnote/javascript-react-typescript-react-refers-to-a-umd-global-but-the-current-file-is-a-module-consider-adding-an-import-instead-justification/

 

Javascript – React + TypeScript: ‘React’ refers to a UMD global but the current file is a module. Consider adding an impor

We are using React and TypeScript in our current project and I come across below behavior. import React, { Component } from 'react'; I replace the above line with the one below since it seems to be using Component import only. import { Component } from 're

itecnote.com

 

 

 

 

728x90