2025.03.12 - [๐ฉ๐ป๐ป dev] - ๐ฌ React import ์ Wrapper ํจํด ํ์ฉํด ๋ณด๊ธฐ
๐ฌ React import ์ Wrapper ํจํด ํ์ฉํด๋ณด๊ธฐ
์ต๊ทผ์ ๋ฆฌ์กํธ์์ import ์์ wrapper ํจํด์ ํ์ฉํ๋ค๋ ๊ธ์ ์ฝ์๋ค.lodash๋ Framer Motion๋ฅผ ์ ์ฒด ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋จ์๋ก importํ๋ ์ํฉ์ด ์ข ์ข ์๋ค๊ณ ํ๋ค.์๋ฅผ ๋ค์ด, lodash๋ฅผ > ๊ณผ ๊ฐ์ด importํ๋ ๊ฒฝ์ฐ
pyotato-dev.tistory.com
์ด์ import ์ wrapper ํจํด์ ํ์ฉํด์ ๋ดค๋ค.
๊ทผ๋ฐ ๋ค์๊ณผ ๊ฐ์ ํ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
์ด์ MotionProps๋ฅผ ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
import { AnimationProps, motion, MotionStyle, SVGMotionProps, Variant, Variants } from 'framer-motion';
// ์ค๋ต
export interface MotionProps {
variants: Variants;
animationProps: AnimationProps;
motionStyle: MotionStyle;
svgProps: SVGMotionProps<SVGPathElement>;
}
๐ ๋ฌธ์ ์์ธ ํ์
"isolatedModules"๋ฅผ ํ๋ก์ ํธ์์ ์ฐพ์๋ณด๋, ์ปดํ์ผ๋ฌ ์ต์ ์ผ๋ก true๋ฅผ ์ค์ ํ๋ฉด, ๋ชจํธํ ๊ฐ/ํ์ ์ importํ ๊ฒฝ์ฐ ์๋ฌ๋ฅผ ๋ธ๋ค. (isolateModules ์ค์ ์ด์ ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ ์ด์ ์ ๋ํด์๋ ๋์ค์ ๋ค๋ค๋ณด๊ณ ์ ํ๋ค.)
isolatedModules๋ ํ์ ์คํฌ๋ฆฝํธ 3.8 ์ดํ์๋ type-only import๊ณผ export ์๋ก์ด ๋ฌธ๋ฒ์ ์ ๊ณตํ๋ค.
๋ค์๊ณผ ๊ฐ์ด import type์ ํ์ annotation๊ณผ declaration๋ก ์ฐ์ผ declaration๋ง importํ๋ค๋ ๊ฒ์ด๋ค.
์ฆ, ๋ฐํ์ ์์๋ ์์ ํ ์ ๊ฑฐ๊ฐ ๋์ด ํ์ ์ด ์์ด์ง๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก export type์ ํ์ ์ฝํ ์คํธ์๋ง ์ฐ์ผ export๋ง ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ ํ์ ์คํฌ๋ฆฝํธ์ output์ ์์ ํ ์ ๊ฑฐ๋๋ค๋ ๊ฒ์ด๋ค.
import type { SomeThing } from "./some-module.js";
export type { SomeThing };
์ค์ํ ๊ฒ์ ํด๋์ค์ ๊ฒฝ์ฐ ๋ฐํ์์ ๊ฐ์ด ์๊ณ , ๋์์ธ ํ์์ ํ์ ์ด ์๊ณ , ์ฝํ ์คํธ์ ๋ฐ๋ผ ์ฐ์์ด ๋ฌ๋ผ์ง๋ค๋ ๊ฒ์ด๋ค.
import type์ ํ์ฉํด ํด๋์ค๋ฅผ import๋ฅผ ํ ๊ฒฝ์ฐ, extend๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ํ์ฉํ ์ ์๊ฒ ๋๋ค.
import type { Component } from "react";
interface ButtonProps {
// ...
}
class Button extends Component<ButtonProps> {
// ~~~~~~~~~
// error! 'Component' only refers to a type, but is being used as a value here.
// โ ๏ธ(Component)๋ ํ์
์ ๊ฐ๋ฆฌํค์ง๋ง ๊ฐ์ผ๋ก ์ฐ์ด๊ณ ์์ต๋๋ค.
// ...
}
Flow๋ฅผ ์ฌ์ฉํด ๋ณธ ๊ฒฝํ์ด ์๋ค๋ฉด ํด๋น ๋ฌธ๋ฒ์ด ์ต์ํ ๊ฒ์ด๋ค.
๋ค๋ง ํ์ ์คํฌ๋ฆฝํธ์์ ์ ๊ณตํ๋ ์ฐจ์ด์ ์ค ํ๋๋ ๋ชจํธํจ์ ์ ๊ฑฐํ๊ธฐ ์ํด ์ ์ฝ์ ์ถ๊ฐํ๋ค๋ ์ ์ด๋ค.
// Is only 'Foo' a type? Or every declaration in the import?
// We just give an error because it's not clear.
import type Foo, { Bar, Baz } from "some-module";
// ~~~~~~~~~~~~~~~~~~~~~~
// error! A type-only import can specify a default import or named bindings, but not both.
import type๊ณผ ํจ๊ป ํ์ ์คํฌ๋ฆฝํธ 3.8๋ ๋ฐํ์์ ์ฐ์ด์ง ์์ import๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํด ์ถ๊ฐ์ ์ผ๋ก ์ปดํ์ผ๋ฌ ํ๋๊ทธ๋ฅผ ์ถ๊ฐํ๋ค : importsNotUsedAsValues
- ํด๋น ํ๋๊ทธ ๊ฐ์ 3๊ฐ ์ค ํ๋๋ก ์ค์ ๊ฐ๋ฅํ๋ค:
- remove : ์ค๋๋ import ์ ๊ธฐ๋ณธ ๋์ ๋ฐฉ์
- preserve: ๊ฐ์ด ์ฐ์ด์ง ์๋ Import ๋ชจ๋ ์ ์ง. import/side-effect๊ฐ ์ ์ง๋๋ ํจ๊ณผ๊ฐ ์๋ค.
- error: preserve์ ๊ฐ์ ์ต์ ์ด์ง๋ง value import๊ฐ ํ์ ์ผ๋ก ์ฐ์ผ ๊ฒฝ์ฐ ์๋ฌ๋ฅผ ๋ธ๋ค. ์ค์๋ก ๊ฐ์ด import๋๋ ๊ฒ์ ๋ฐฉ์งํ๋ฉด์ side-effect import๊ฐ explicitํ๋๋ก ์ฐ์ผ ์ ์๋ค.
๊ณต์ ๋ฌธ์๋ฅผ ์ข ํฉํด ๋ณด๋ MotionProps๋ ํ์ ์ผ๋ก ์ฐ๊ณ isolatedModules๋ฅผ true๋ผ๊ณ ์ค์ ํด ๋ ์ํฉ์์,
๋ํดํธ๋ก ํ์ ์ด๋ผ๋ ์ ๋ณด๋ฅผ ์ ๊ฑฐํด ๋ฒ๋ฆฌ๋ฏ๋ก ๋ค์ exportํ๋๋ฐ ๊ฐ์ผ๋ก exportํ๋ ค๋ ๋ถ์ผ์น ๋๋ฌธ์ ๋ฐ์ํ ๊ฑฐ ๊ฐ๋ค.
๐ Solution
ํ์ ์ผ๋ก ์ฐ์ผ MotionProps์ ํด๋น ์ ๋ณด๋ฅผ type ํค์๋๋ก ๋ช ์ํด ์ฃผ๋ฉด ๋๋ค.
export { classNames, Motion, MotionProps }; //before
export { classNames, Motion, type MotionProps }; //after
References
Documentation - TypeScript 3.8
TypeScript 3.8 Release Notes
www.typescriptlang.org
Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.ts
I have created multiple interfaces and want to ship them all from a common index.ts file as shown below: --pages ------index.interface.ts --index.ts Now In my index.ts I am exporting something like:
stackoverflow.com