Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ 코드리뷰용 pr ] typescript로 마이그레이션 #20

Open
wants to merge 35 commits into
base: 5wintaek/ts-refactor
Choose a base branch
from

Conversation

5wintaek
Copy link
Collaborator

@5wintaek 5wintaek commented Jun 7, 2024

✅ 작업 내용

  • ts 및 기타 모듈 설치
  • tsconfig.json, tsconfig.node.json 파일 추가
  • 타입 정의
  • 정의한 타입 입히기

📌 이슈 사항

1️⃣ Common,Component용 Type을 Types라는 폴더에 지정

  • 관심사의 분리를 통해 타입 지정 폴더를 분리하여 타입별로 지정하였습니다.

2️⃣ any 사용 금지

  • 타입 지정할 때 any 사용 절~때 금지 지켰습니다!

3️⃣ Early Return

  • mainContents는 퀴즈 내용에 해당하는 변수로, 항상 존재해야 하는 값이라 mainContents가 없는 경우에 얼리리턴 해줬습니다.
// QuizPage.tsx
if (!mainContents) return;
  • 나중에 에러페이지를 만들게 된다면 값이 없는 경우 에러페이지로 보내버리는게 더 좋을 듯 하네요 !

✍ 궁금한 것

1️⃣ React.ReactElement vs JSX.Element

Router 파일을 마이그레이션 하다가 Router 쪽에 타입 지정을 하라는 오류가 발생하였고, React.ReactElement 와 JSX.Element 둘 다 사용했을떄 오류가 없길래 둘의 차이가 뭘지 궁금해서 찾아봤습니다. (가볍게 찾아본것)

  • JSX.ElementReact.ReactElement는 비슷한 용도로 사용됩니다. 사실, JSX.Element는 TypeScript의 JSX 네임스페이스에 정의된 타입으로, 대부분의 경우 React.ReactElement와 동일합니다.
  • ReactElement는 더 일반적인 타입이며, React 요소를 보다 넓게 포함합니다.

2️⃣ vite-env.d.ts

처음 마이그레이션 진행 후, 타입 지정하라는 오류가 뜨질 않아 고생을 엄청나게 했습니다. vite->TS마이그레이션 을 할 떄, 꼭 참고해주시길 바랍니다 !

@5wintaek 5wintaek added ⚒️ refactor 코드 리팩토링 🐡 taek 승택 labels Jun 7, 2024
@5wintaek 5wintaek changed the title [TS 마이그레이션] 코드리뷰용 PR [ 코드리뷰용 pr ] typescript로 마이그레이션 Jun 7, 2024
Copy link
Member

@Arooming Arooming left a comment

Choose a reason for hiding this comment

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

LGTM !! 👊👊

Comment on lines +37 to +43
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
},
};
Copy link
Member

Choose a reason for hiding this comment

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

이건 무슨 의미인가요 !!?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

이 설정은 ESLint가 import 문을 분석할 때 사용하는겁니당. .extension는 파일 확장자를 의미하는거고, 파일을 읽어들일떄 .jsx .js .ts .tsx 확장자를 가진 파일들을 해석하는는 뜻

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

resolve: {
    extensions: ['.ts', '.tsx'], // 여기서 .ts와 .tsx 확장자에 .을 추가합니다.
    alias: [{ find: '@', replacement: resolve(__dirname, './src') }],
  },

vite에서 TS를 사용할 떄 기본적으로 extensions 쪽에 .ts ,.tsx 에 확장자를 추가하는것을 권장합니다.

eslint에서의 설정은 제가 타입오류지정을 해결하면서 eslint-plugin을 같이 깔면서 설정해둔것 같습니다. 굳이 없어도 되는 확장자 입니다

Comment on lines +5 to +8
export interface RetryButtonProps {
children: React.ReactNode;
onClick: () => void;
}
Copy link
Member

Choose a reason for hiding this comment

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

ChildrenProps가 많이 쓰이고 있는데 아래처럼 상속시켜주면 중복 코드를 줄일 수 있을 것 같아요 !

Suggested change
export interface RetryButtonProps {
children: React.ReactNode;
onClick: () => void;
}
export interface RetryButtonProps extends ChildrenProps {
onClick: () => void;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

감사합니다 !

@@ -8,23 +8,37 @@ import ResultPage from './pages/ResultPage';
import SelectPage from './pages/SelectPage';
import OnboardingPage from './pages/WaitingPage';

const Router = () => {
const [homeComponent, setHomeComponent] = useState(<OnboardingPage />);
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
Copy link
Member

Choose a reason for hiding this comment

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

여기서 eslint를 제한한 이유가 있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

처음에 Router () 부분쪽에 빨간줄이 떠서 eslint 제한을 걸어놨는데, 타입지정을 해줘서 지금은 지워도 상관없네요 ! 수정해서 다시 올리겠습니다

@@ -3,11 +3,12 @@ import { IcCloseGray, ImgHint } from '../../assets';
// import { useEffect } from 'react';
Copy link
Member

Choose a reason for hiding this comment

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

안쓰는 주석은 지워주세용 .

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

네엡 수정해서 올리겠습니다

@@ -33,7 +33,7 @@ const SelectPage = () => {
sessionStorage.setItem('quizId', quizId);
}
if (content) {
sessionStorage.setItem('contents', content);
sessionStorage.setItem('contents', content.content);
Copy link
Member

Choose a reason for hiding this comment

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

content 정의를 아래처럼 해주면 content로만 사용할 수 있어요 !

Suggested change
sessionStorage.setItem('contents', content.content);
const { content } = await getQuizContent(quiz)'
sessionStorage.setItem('contents', content);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 객체분할 대신 사용해주셔서 감사합니다 ! 수정해서 다시 올리겠습니다

Comment on lines +4 to +6
interface WaitingProps {
isLoadingPage: boolean;
}
Copy link
Member

Choose a reason for hiding this comment

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

타입파일 따로 빼줬으니까 얘도 분리하면 좋을 것 같아용 !!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

아 깜빡하고 분리를 하지 않았네요! 알겠습니다~~!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚒️ refactor 코드 리팩토링 🐡 taek 승택
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants