[황수정] 스프린트 11#47
Merged
loquemedalagana merged 3 commits intocodeit-sprint-fullstack:express-황수정from Jun 21, 2025
Hidden character warning
The head ref may contain hidden characters: "express-\ud669\uc218\uc815-sprint11"
Merged
Conversation
added 2 commits
June 17, 2025 11:35
| getFavoriteCount() { | ||
| return this._likes.length; | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
해당 코드는 JavaScript 스타일에 가까우며, TypeScript 클래스의 캡슐화 및 타입 안정성을 강화하기 위해 아래와 같이 개선할 수 있습니다.
✅ 리팩토링 방향 요약
| 항목 | 현재 상태 | 개선 방향 |
|---|---|---|
| 필드 선언 | 암묵적 타입 (e.g. _id;) | private _id: string; 등 명시적 타입 사용 |
| 접근 제한자 | 없음 | private, readonly, public 등으로 캡슐화 |
| 생성자 param | 직접 할당 | 인터페이스로 타입 보장 |
| Getter | OK | 유지 가능, 혹은 readonly 속성으로 대체 가능 |
🔧 리팩토링 예시
import { TArticleParam } from "@/types/article";
import { TLikeParam } from "@/types/like";
export class Article {
private readonly _id: string;
private readonly _writerId: number;
private readonly _title: string;
private readonly _content: string;
private readonly _image?: string;
private readonly _createdAt: string;
private readonly _updatedAt: string;
private readonly _likes: TLikeParam[];
constructor(param: TArticleParam) {
this._id = param.id;
this._writerId = param.writerId;
this._title = param.title;
this._content = param.content;
this._image = param.image;
this._createdAt = param.createdAt;
this._updatedAt = param.updatedAt;
this._likes = param.likes ?? [];
}
get id(): string {
return this._id;
}
get writerId(): number {
return this._writerId;
}
get title(): string {
return this._title;
}
get content(): string {
return this._content;
}
get image(): string | undefined {
return this._image;
}
get createdAt(): string {
return this._createdAt;
}
get updatedAt(): string {
return this._updatedAt;
}
getFavoriteCount(): number {
return this._likes.length;
}
isFavoritedBy(userId: number): boolean {
if (!userId) return false;
return this._likes.some((like) => like.userId === userId);
}
}📌 정리
- 접근 제한자 (
private,readonly) 사용은 TypeScript의 강점 중 하나로, 외부에서 직접 필드 변경을 막을 수 있음. - Getter 메서드는 그대로 유지해도 되고, ES6 Getter(
get) 문법으로 자연스럽게 노출 가능. - 네이밍도
getXxx()대신xxx나isFavoritedBy()등 의미 중심으로 리팩토링 가능. - 타입 명시 강화:
string,number,undefined등을 명확히 명시하여 타입 안정성 확보.
| /** | ||
| * 현재 시각으로부터 1시간동안 유효한 액세스 토큰을 생성합니다. | ||
| */ | ||
| static buildAccessToken(payload: any) { |
Contributor
There was a problem hiding this comment.
any 타입은 피하고 명시적인 인터페이스와 타입가드를 사용해 주세요. payload, authorizationHeaderValue, jwt.decode()의 반환값 등에 모두 명확한 타입 지정을 권장합니다.
Contributor
|
전체적으로 배운 내용 및 새로운 부분들 정리 너무 잘 해주셨어요. any사용 등 javascript 스타일이 남아있는게 아쉽지만 generic 부분 및 union, Pick 등도 한번 알아보세요! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
정답 코드를 활용해 진행했습니다.
@loquemedalagana
요구사항
기본
백엔드 요구사항
멘토에게