-
Notifications
You must be signed in to change notification settings - Fork 22
[황수정] 스프린트 11 #47
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
[황수정] 스프린트 11 #47
The head ref may contain hidden characters: "express-\uD669\uC218\uC815-sprint11"
Conversation
| getFavoriteCount() { | ||
| return this._likes.length; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 코드는 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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any 타입은 피하고 명시적인 인터페이스와 타입가드를 사용해 주세요. payload, authorizationHeaderValue, jwt.decode()의 반환값 등에 모두 명확한 타입 지정을 권장합니다.
|
전체적으로 배운 내용 및 새로운 부분들 정리 너무 잘 해주셨어요. any사용 등 javascript 스타일이 남아있는게 아쉽지만 generic 부분 및 union, Pick 등도 한번 알아보세요! |
정답 코드를 활용해 진행했습니다.
@loquemedalagana
요구사항
기본
백엔드 요구사항
멘토에게