같은 종류의 함수는 반환 타입 통일하기 | Frontend Fundamentals #108
Replies: 2 comments 4 replies
-
|
Beta Was this translation helpful? Give feedback.
4 replies
-
유효성 검사 함수와 같이 동일한 맥락의 로직에서 반환 타입을 일관되게 관리한다는 측면에서 type ValidationCheckReturnType = { ok: true } | { ok: false; reason: string };
abstract class Validator {
abstract validate(value: unknown): ValidationCheckReturnType;
protected success(): ValidationCheckReturnType {
return { ok: true };
}
protected failure(reason: string): ValidationCheckReturnType {
return { ok: false, reason };
}
} class AgeValidator extends Validator {
validate(age: number): ValidationCheckReturnType {
if (!Number.isInteger(age)) {
return this.failure("나이는 정수여야 해요.");
}
if (age < 18) {
return this.failure("나이는 18세 이상이어야 해요.");
}
if (age > 99) {
return this.failure("나이는 99세 이하이어야 해요.");
}
return this.success();
}
} 특히 필드값 변경에 조금 더 유연하게 대처할 수 있는 장점이 있을 것 같아요 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
같은 종류의 함수는 반환 타입 통일하기 | Frontend Fundamentals
변경하기 쉬운 프론트엔드 코드를 위한 지침서
https://frontend-fundamentals.com/code/examples/use-user.html
Beta Was this translation helpful? Give feedback.
All reactions