Skip to content

Conversation

@afafmmm
Copy link
Collaborator

@afafmmm afafmmm commented Jun 17, 2025

요구사항

공통 요구사항

  • Github에 위클리 미션 PR을 만들어 주세요.
  • React 및 Express를 사용해 진행합니다.
  • TypeScript를 활용해 프로젝트의 필요한 곳에 타입을 명시해 주세요.
  • any 타입의 사용은 최소화해 주세요.
  • 복잡한 객체 구조나 배열 구조를 가진 변수에 인터페이스 또는 타입 별칭을 사용하세요.
  • Union, Intersection, Generics 등 고급 타입을 적극적으로 사용해 주세요.
  • 타입 별칭 또는 유틸리티 타입을 사용해 타입 복잡성을 줄여주세요.
  • 타입스크립트 컴파일러가 에러 없이 정상적으로 작동해야 합니다.

백엔드 요구사항

  • 기존 Express.js 프로젝트를 타입스크립트 프로젝트로 마이그레이션 해주세요
  • tsconfig.json 파일을 생성하고, 필요한 컴파일러 옵션을 설정해야 합니다. (예: outDir)
  • TypeScript 관련 명령어를 package.json에 설정해 주세요. (예: 빌드 및 개발 서버 실행 명령어).
  • ts-node와 nodemon을 사용하여 개발 환경을 구성합니다.
  • nodemon과 함께 ts-node를 사용하여 . ts 파일이 변경될 때 서버를 자동으로 재시작하도록 설정합니다.
  • Mongoose나 Prisma 등 ORM을 사용하는 경우, 모델에 대한 인터페이스 또는 타입을 정의합니다
  • 필요한 경우, declare를 사용하여 타입을 오버라이드하거나 확장합니다.

주요 변경사항

스크린샷

멘토에게

  • 셀프 코드 리뷰를 통해 질문 이어가겠습니다.


app.use(
cors({
origin: "http://localhost:3001", // 프론트엔드 주소
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요 부분도 환경변수로 바꿔주세요!

res: Response,
next: NextFunction
) => Promise<any>;

Copy link
Contributor

@loquemedalagana loquemedalagana Jun 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generic 으로 바꿔 보시는거 어때요?

✅ Generic 버전 예시 (T를 반환 타입으로 지정)

import { Request, Response, NextFunction } from "express";

type AsyncFunction<T = any> = (
  req: Request,
  res: Response,
  next: NextFunction
) => Promise<T>;

export const asyncHandler =
  <T = any>(fn: AsyncFunction<T>) =>
  (req: Request, res: Response, next: NextFunction): void => {
    Promise.resolve(fn(req, res, next)).catch(next);
  };


🧠 설명

  • Tfn무엇을 반환하는지 타입을 명시하기 위한 제네릭입니다.
  • 기본값을 any로 설정해둬서 명시하지 않아도 동작은 하되, 필요한 경우 특정 반환 타입을 명시할 수 있습니다.

예를 들어:

const getUser = asyncHandler<{ name: string }>(async (req, res) => {
  const user = await fetchUser();
  res.json(user);
});


✨ 이점

  • API 응답 타입을 명확히 하고자 할 때 도움이 됩니다.
  • TypeScript 환경에서 컨트롤러 리턴값을 추론하거나 추적할 때 용이합니다.
  • 필요시 RequestResponse 타입도 제네릭으로 확장 가능하게 커스터마이즈할 수 있습니다.

async function findByProductId(
productId: number,
options: CommentRepositoryPaginationOptions = {}
): Promise<CommentWithWriter[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요 부분이 generic 적용 예시인거 익혀주세요!

);
},
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅참고사항: storage.destination, filename은 반드시 콜백 내에서 동기적으로 처리해야 합니다.

@loquemedalagana loquemedalagana merged commit e87b6da into codeit-sprint-fullstack:main Jun 21, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants