npm i @types/node @types/react @types/react-dom @types/jest
{
"compilerOptions": {
"target": "ES5",
"module": "CommonJS",
"strict": true,
"allowJs": true,
},
"include": ["src"]
}
이때,
"~~ 파일은 입력 파일을 덮어쓰므로 쓸 수 없습니다."
라는 에러가 발생한다.
.jsx
로 변경한다.tsx
로 바꿔준다.
"기본 내보내기가 없습니다."
라는 에러가 발생하면 tsconfig.json에"esModuleInterop": true
를 추가한다.
"--jsx 플래그를 제공하지 않으면 JSX를 사용할 수 없습니다."
라는 에러가 발생하면 tsconfig.json에"jsx": "react-jsx"
를 추가한다.
index.tsx에서 document.getElementId("root") 부분에 에러가 발생하면
document.getElementById("root") as HTMLElement
로 바꾸어준다.
{
"compilerOptions": {
"target": "ES5",
"module": "CommonJS",
"strict": true,
"allowJs": true,
"esModuleInterop": true,
"jsx": "react-jsx"
},
"include": ["src"]
}
확장자 정의 파일 생성 (declarations)
declare module "*.png" {
const value: string;
export default value;
}
export interface ImageInfo {
id: number;
download_url: string;
author: string;
private_note: string;
}
{savedImages.map((el: ImageInfo) => (
<Figure
key={el.id}
onMouseEnter={() => setHoveredIndex(el.id)}
onMouseLeave={() => setHoveredIndex(-1)}
>
{hoveredIndex === el.id && (
<EditIcon
src={edit}
onClick={() => {
setIsModal(true);
setClickedIndex(el.id);
}}
/>
)}
<Img
key={el.id}
src={el.download_url}
isHover={hoveredIndex === el.id ? 1 : 0}
/>
const Img = styled.img`
width: 100%;
border-radius: 18px;
cursor: pointer;
transition: filter 100ms ease;
filter: ${(props) =>
props.isHover === 1 ? "brightness(55%)" : "brightness(100%)"};
`;
interface ImgProps {
isHover: number;
}
const Img = styled.img<ImgProps>`
width: 100%;
border-radius: 18px;
cursor: pointer;
transition: filter 100ms ease;
filter: ${(props) =>
props.isHover === 1 ? "brightness(55%)" : "brightness(100%)"};
`;
const handleSaveClick = (imgInfo: ImageInfo) => {};
const EditModal = ({ setIsModal, idx }: { setIsModal: any; idx: number }) => {}
CommonTypes.ts
export interface ImageInfo {
id: number;
download_url: string;
author: string;
private_note: string;
}
export interface ImgProps {
isHover: number;
}
export interface Save {
save: number;
}
export interface IsMobile {
mobile: number;
}
아래처럼 import해서 사용하면 된다.
import { ImageInfo, ImgProps } from "../CommonTypes";
참고 | 루트 디렉토리에 있는 파일은 tsconfig.json에 아래와 같이 파일 경로를 추가해준다.
{ "compilerOptions": { "target": "ES5", "module": "CommonJS", "strict": true, "allowJs": true, "esModuleInterop": true, "jsx": "react-jsx" }, "include": ["src", "declaration.d.ts", "CommonTypes.ts"] }
data를 아래와 같이 일반 배열로 초기화해두면 data 내부 값의 타입
은 정해지지 않았으므로, 내부 요소를 사용할 때마다 아래와 같이 타입을 매번 지정해주어야 한다.
const [data, setData] = useState([]); // 이미지 데이터
{savedImages.map((el: ImageInfo) => (
<Figure
key={el.id}
onMouseEnter={() => setHoveredIndex(el.id)}
onMouseLeave={() => setHoveredIndex(-1)}
>
{hoveredIndex === el.id && (
<EditIcon
src={edit}
onClick={() => {
setIsModal(true);
setClickedIndex(el.id);
}}
/>
)}
이런 번거로움을 덜기 위해 dat의 타입을 배열로 지정하고, 배열 내부 값의 타입을 ImageInfo로 설정할 수 있다.
const [data, setData] = useState<ImageInfo[]>([]); // 이미지 데이터
업로드되는 파일의 개수가 너무 많아지기 때문에 gitignore 해주어야 한다.
# node_modules 디렉토리 무시
node_modules/
git add .gitignore
git commit -m "Add .gitignore to exclude node_modules"
git rm -r --cached node_modules
git add .
git commit -m "Remove node_modules and update .gitignore"
git push origin