interface๋ type์ผ๋ก ์ ์ํ์ฌ ์ธ์ ์๋๋ฐ d.ts๋ก ์ฌ์ฉํ๋ ์ด์ ๋ํ์ฌ
ํ์
์คํฌ๋ฆฝํธ์์ interface๋ type์ ์ฌ์ฉํด ํ์
์ ์ ์ํ ์ ์๋๋ฐ๋ .d.ts ํ์ผ์ ์ฌ์ฉํ๋ ์ด์ ๋ ์ฌ์ฉ ๋ชฉ์ ๊ณผ ๋งฅ๋ฝ์ ์ฐจ์ด์ ์์ต๋๋ค. .d.ts ํ์ผ์ ์ฃผ๋ก ์ ์ธ ํ์ผ(Declaration File)๋ก์ ํน์ ํ ์ญํ ์ ์ํํ๋ฉฐ, ์ผ๋ฐ์ ์ธ interface๋ type ์ ์์๋ ๋ค๋ฅธ ์ํฉ์์ ์ ์ฉํฉ๋๋ค. ์๋์์ .d.ts๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์ ๊ทธ ์ฅ์ ์ ๊ตฌ์ฒด์ ์ผ๋ก ์ค๋ช
ํ๊ฒ ์ต๋๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ์์ ํตํฉ: ๊ธฐ์กด ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํ์ ์ ์ถ๊ฐํ ๋.
- ์ ์ธ๊ณผ ๊ตฌํ ๋ถ๋ฆฌ: ํ์ ์ ์๋ฅผ ๋ณ๋๋ก ๊ด๋ฆฌํ๊ณ ๋ฐํ์ ์ฝ๋์ ์ํฅ์ ์ฃผ์ง ์์ผ๋ ค ํ ๋.
- ๊ธ๋ก๋ฒ ํ์ ์ ๊ณต: ํ๋ก์ ํธ ์ ์ฒด์์ ์ฌ์ฉํ ๊ณตํต ํ์ ์ ์ ์ญ์ ์ผ๋ก ์ ์ธํ ๋.
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐฐํฌ: ํ์ ์คํฌ๋ฆฝํธ ์ฌ์ฉ์๋ฅผ ์ํด ํ์ ์ ์๋ฅผ ์ ๊ณตํ ๋.
๋ฐ๋ฉด, ํ๋ก์ ํธ ๋ด๋ถ์์๋ง ํ์
์ ์ ์ํ๊ณ ์ฌ์ฉํ ๋๋ interface๋ type์ .ts ํ์ผ์ ์์ฑํ๋ ๊ฒ์ด ๋ ๊ฐ๋จํ๊ณ ์ ํฉํฉ๋๋ค. .d.ts๋ ํนํ ์ธ๋ถ์์ ์ธํฐํ์ด์ค๋ฅผ ๋ค๋ฃฐ ๋ ๋น์ ๋ฐํ๋ ๋๊ตฌ๋ผ๊ณ ๋ณผ ์ ์์ต๋๋ค.
๊ฐ๋จํ ์๋ฐ์คํฌ๋ฆฝํธ ํจ์์ ํ์ ์ ์ ์ถ๊ฐ
๊ธฐ์กด ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ์ ํ์
์ด ์๋ ์ํฉ์ ๊ฐ์ ํ๊ณ , .d.ts ํ์ผ๋ก ํ์
์ ์ ๊ณตํ๋ ์์ ์
๋๋ค.
๐ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ (math.js)
function add(a, b) { return a + b; } module.exports = { add };
๐ ํ์ ์ ์ธ ํ์ผ (math.d.ts)
export function add(a: number, b: number): number;
๐ ํ์ ์คํฌ๋ฆฝํธ์์ ์ฌ์ฉ (app.ts)
import { add } from "./math"; const result = add(5, 3); // result๋ number ํ์ ์ผ๋ก ์ถ๋ก ๋จ console.log(result); // 8 // add("5", "3"); // ์ค๋ฅ: 'string' ํ์ ์ 'number' ํ์ ์ ํ ๋นํ ์ ์์
math.js๋ ํ์
์ ๋ณด๊ฐ ์๋ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ์ด์ง๋ง, math.d.ts์์ add ํจ์์ ๋งค๊ฐ๋ณ์์ ๋ฐํ ํ์
์ ์ ์ํด์ค๋๋ค. ํ์
์คํฌ๋ฆฝํธ๋ ์ด ์ ์ธ ํ์ผ์ ์ฐธ์กฐํ์ฌ ํ์
๊ฒ์ฌ๋ฅผ ์ํํ๋ฉฐ, ์๋ชป๋ ํ์
์ฌ์ฉ ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํต๋๋ค. ํ์
์ ์๋ ์ปดํ์ผ ์ ํ์
์ฒดํฌ์ ์ฌ์ฉ๋๊ณ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋ณํ๋ ๋ฐํ์ ์ฝ๋์๋ ํ์
์ด ์ง์ ํฌํจ๋์ง ์์ต๋๋ค. ์ค์ ์คํ๋๋ ๋ก์ง๋ง ๋จ์ต๋๋ค.
Lodash์ ๊ฐ์ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํตํฉ
Lodash๋ ์ธ๊ธฐ ์๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ ํธ๋ฆฌํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. ํ์
์คํฌ๋ฆฝํธ์์ ์ด๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด @types/lodash์ ๊ฐ์ ํ์
์ ์ ํจํค์ง๋ฅผ ์ค์นํ๊ฑฐ๋ ์ง์ .d.ts ํ์ผ์ ์์ฑํ ์ ์์ต๋๋ค.
๐ Lodash ์ค์น
npm install lodash npm install --save-dev @types/lodash //ํ์ ์ ์ ์ค์น (DefinitelyTyped ์ ๊ณต)
๐ ํ์ ์คํฌ๋ฆฝํธ์์ ์ฌ์ฉ
import _ from "lodash"; const numbers = [1, 2, 3, 4]; const sum = _.sum(numbers); // sum์ number ํ์ ์ผ๋ก ์ถ๋ก ๋จ console.log(sum); // 10 // _.sum("not an array"); // ์ค๋ฅ: 'string'์ 'ArrayLike<number>' ํ์ ์ ํ ๋นํ ์ ์์
@types/lodash ํจํค์ง๋ Lodash์ ๋ชจ๋ ํจ์์ ๋ํ ํ์
์ ์๋ฅผ ์ ๊ณตํฉ๋๋ค. ์๋ฅผ ๋ค์ด, _.sum์ ์ซ์ ๋ฐฐ์ด์ ๋ฐ์ ์ซ์๋ฅผ ๋ฐํํ๋ ํจ์๋ก ํ์
์ด ์ ์๋์ด ์์ผ๋ฉฐ, ํ์
์คํฌ๋ฆฝํธ๋ ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์
๊ฒ์ฌ๋ฅผ ์ํํฉ๋๋ค.
React ์ปดํฌ๋ํธ์ ํตํฉ
React๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์์ฑ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ง๋ง, ํ์
์คํฌ๋ฆฝํธ์์ ์ฌ์ฉํ๋ ค๋ฉด ํ์
์ ์๊ฐ ํ์ํฉ๋๋ค.
@types/react์ @types/react-dom์ ํตํด ์ด๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
@types/react์ @types/react-dom์ ํตํด ์ด๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
๐ React ์ค์น
npm install react react-dom npm install --save-dev @types/react @types/react-dom
๐ ํ์ ์คํฌ๋ฆฝํธ๋ก React ์ปดํฌ๋ํธ ์์ฑ
import React from "react"; import ReactDOM from "react-dom"; interface Props { name: string; } const Greeting: React.FC<Props> = ({ name }) => { return <h1>Hello, {name}!</h1>; }; ReactDOM.render(<Greeting name="Alice" />, document.getElementById("root")); // <Greeting name={123} /> // ์ค๋ฅ: 'number' ํ์ ์ 'string' ํ์ ์ ํ ๋นํ ์ ์์
@types/react๋ React์ ํ์
์ ์๋ฅผ ์ ๊ณตํ๋ฉฐ, React.FC์ ๊ฐ์ ํ์
์ ํตํด ํจ์ํ ์ปดํฌ๋ํธ์ props ํ์
์ ์ ์ํ ์ ์์ต๋๋ค. name props๊ฐ string์ผ๋ก ์ ์๋์์ผ๋ฏ๋ก, ์ซ์๋ฅผ ์ ๋ฌํ๋ฉด ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
React์ ์ปค์คํ ํ์ ์ ์ถ๊ฐํ๊ณ ์ถ๋ค๋ฉด ์ปค์คํ .d.ts๋ก ํ์ฅ (์ ํ์ )
๋ง์ฝ React์ ์ปค์คํ
ํ์
์ ์ถ๊ฐํ๊ณ ์ถ๋ค๋ฉด, ๋ค์๊ณผ ๊ฐ์ด custom.d.ts ํ์ผ์ ์์ฑํ ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ customProp์ React ์ปดํฌ๋ํธ์์ ํ์
์์ ํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๐ custom.d.ts
declare module "react" { interface CustomComponentProps { customProp: boolean; } }
๐ CustomComponent.tsx
import React from "react"; const CustomComponent: React.FC<CustomComponentProps> = ({ customProp }) => { return <div>{customProp ? "Yes" : "No"}</div>; }; export default CustomComponent;
๐ App.tsx
import React from "react"; import CustomComponent from "./CustomComponent"; const App: React.FC = () => { return ( <CustomComponent customProp={true} /> ); }; export default App;
์ปค์คํ .d.ts๋ก ํ์ฅ
ํ์
์คํฌ๋ฆฝํธ๊ฐ ์ธ์ํ์ง ๋ชปํ๋ ํ์
์ด๋ ํ์
์คํฌ๋ฆฝํธ ๋ด์์ ์ฌ์ฉํ ํ์
๋ค์ ์ ์ ํ ๋ ์๋ก svg ํ์ผ์ ํ์
์คํฌ๋ฆฝํธ์์ ๋ถ๋ฌ์ฌ ์ ์๊ฒ ํ๋๋ก custom.d.ts ํ์ผ์ ์์ฑํด์ค๋ค.
๐ custom.d.ts
declare module "*.svg" { import * as React from "react"; interface CustomSVGProps { title?: string; } // SVG ์ปดํฌ๋ํธ์ ์ ์ฒด props ํ์ ์ ์ type SVGComponentProps = React.SVGProps<SVGSVGElement> & CustomSVGProps; // ReactComponent๋ฅผ ํจ์ํ ์ปดํฌ๋ํธ๋ก ์ ์ธ export const ReactComponent: React.FunctionComponent<SVGComponentProps>; // SVG ํ์ผ์ ๊ธฐ๋ณธ ๋ด๋ณด๋ด๊ธฐ๋ก ๋ฌธ์์ด URL ์ ์ const path: string; export default path; }
์ด ์ ์ธ์ ์ฌ์ฉํ๋ฉด SVG ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ธ์ ํ์ฉํ ์ ์์ต๋๋ค
import { ReactComponent as Icon } from "./icon.svg"; import iconPath from "./icon.svg"; const App: React.FC = () => ( <div> <Icon width="50" height="50" fill="blue" title="My Icon" /> <img src={iconPath} alt="Icon" width="50" height="50" /> </div> );
- Icon์ SVGComponentProps ํ์ ์ ๊ธฐ๋ฐ์ผ๋ก ํ์ ๊ฒ์ฌ๊ฐ ์ด๋ฃจ์ด์ง๋ฉฐ, title์ ์ต์ ๋๋ก ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
- iconPath๋ string ํ์ ์ผ๋ก ์ด๋ฏธ์ง ์์ค๋ก ํ์ฉ๋ฉ๋๋ค.
๊ตฌํ ์ฝ๋์ ํ์ ์ ์์ ๋ถ๋ฆฌ
ํ์
์ ์์ ์ค์ ๊ตฌํ ์ฝ๋๋ฅผ ๋ถ๋ฆฌํ๊ณ ์ถ์ ๋, .d.ts ํ์ผ์ ์ ์ธ๋ง ํฌํจํ๊ณ ์ด๋ ๋ฐํ์์ ์ํฅ์ ์ฃผ์ง ์์ต๋๋ค. ๋ฐ๋ฉด .ts ํ์ผ์ interface๋ type๊ณผ ํจ๊ป ๊ตฌํ ์ฝ๋๋ฅผ ์์ผ๋ฉด ์ปดํ์ผ ์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ก ๋ณํ๋์ด ๋ฐํ์์ ํฌํจ๋ฉ๋๋ค.
๐ .ts ํ์ผ์ ๋ชจ๋ ์์ฑ(math.ts)
interface MathOperation { (a: number, b: number): number; } const add: MathOperation = (a, b) => a + b; export { add };
math.ts ์ปดํ์ผ ๊ฒฐ๊ณผ: interface๋ ์ฌ๋ผ์ง๊ณ ๊ตฌํ ์ฝ๋๋ง ๋จ์.
๐ math.d.ts
export interface MathOperation { (a: number, b: number): number; } export const add: MathOperation;
๐ math.js
const add = (a, b) => a + b; module.exports = { add }; export const add: MathOperation;
์ ์ธ๊ณผ ๊ตฌํ์ ๋ถ๋ฆฌํ๋ฉด ์ค์ ์คํ๋๋ ๋ก์ง(๊ตฌํ)์ ๋ณ๋์ ์์ค ํ์ผ์ ๋๊ณ ํ์
์ ์๋ ์ฃผ๋ก ์ปดํ์ผ ํ์์ ์ฌ์ฉ๋์ด ์ฝ๋์ ์ ํฉ์ฑ์ ํ์ธํ๊ฑฐ๋ ๊ฐ๋ฐ์์๊ฒ ํํธ๋ฅผ ์ฃผ๋ ์ญํ ์ ํ์ง๋ง, ๋ฐํ์์ ์คํ๋๋ ๊ธฐ๊ณ์ด ์ฝ๋๋ ํ๋ก๊ทธ๋จ์ ๋์์๋ ์ง์ ์ ์ธ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค. ํ์
์ ์๋ง ๊ณต์ ํ๊ฑฐ๋ ์ฌ์ฌ์ฉํ๊ธฐ ์ฌ์์ง๋๋ค. ํนํ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํตํฉํ ๋ ์ ์ฉํฉ๋๋ค.
interface/type๊ณผ .d.ts์ ์ฐจ์ด์ ์์ฝ
ํน์ง | ์ฌ์ฉ๋ฒ์ | ๊ตฌํ ํฌํจ ์ฌ๋ถ | ๋ชจ๋ ํตํฉ | ์ ์ญ ์ ์ธ | ๋ชฉ์ |
---|---|---|---|---|---|
interface / type | ํ์ ์คํฌ๋ฆฝํธ ์ฝ๋ ๋ด๋ถ์์๋ง ์ ํจ | .ts ํ์ผ ๋ด์์ ๊ตฌํ๊ณผ ํจ๊ป ์ฌ์ฉ ๊ฐ๋ฅ | ์ธ๋ถ ๋ชจ๋์ ํ์ ์์ ๋ถ๊ฐ | import/export ํ์ | ํ๋ก์ ํธ ๋ด ํ์ ์ ์ |
.d.ts ํ์ผ | ์๋ฐ์คํฌ๋ฆฝํธ์ ํ์ ์คํฌ๋ฆฝํธ ์ฐ๊ฒฐ ๊ฐ๋ฅ | ์ ์ธ๋ง ํฌํจ, ๊ตฌํ์ ๋ณ๋ ํ์ผ์์ | declare module๋ก ์ธ๋ถ ๋ชจ๋ ํ์ ์ ์ ๊ฐ๋ฅ | ์ ์ญ ํ์ ์ ์ ๊ฐ๋ฅ (declare ์ฌ์ฉ) | ์ธ๋ถ ์ฝ๋์์ ์ธํฐํ์ด์ค ์ญํ |