GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: discriminated 타입에 대한 코드 추가 · dev-language/TypeScript_prac@f84d79e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • 1-types
    • 1-6-discriminated.js
    • 1-6-discriminated.ts
  • docs
    • 02. 타입.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎1-types/1-6-discriminated.js‎

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
function printLoginState(state) {
3+
if (state.result === "success") {
4+
console.log("\uD83C\uDF89 ".concat(state.response.body));
5+
} else {
6+
console.log("\uD83D\uDE22 ".concat(state.msg));
7+
}
8+
}
9+
// Success 타입의 값 전달 예시
10+
var successState = {
11+
result: "success",
12+
response: {
13+
body: "로그인에 성공하였습니다.",
14+
},
15+
};
16+
printLoginState(successState);
17+
// Fail 타입의 값 전달 예시
18+
var failState = {
19+
result: "fail",
20+
msg: "로그인에 실패하였습니다.",
21+
};
22+
printLoginState(failState);
23+
}
Collapse file

‎1-types/1-6-discriminated.ts‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
type Success = {
3+
result: "success";
4+
response: {
5+
body: string;
6+
};
7+
};
8+
9+
type Fail = {
10+
result: "fail";
11+
msg: string;
12+
};
13+
14+
type LoginState = Success | Fail;
15+
16+
function printLoginState(state: LoginState) {
17+
if (state.result === "success") {
18+
console.log(`🎉 ${state.response.body}`);
19+
} else {
20+
console.log(`😢 ${state.msg}`);
21+
}
22+
}
23+
24+
// Success 타입의 값 전달 예시
25+
const successState: Success = {
26+
result: "success",
27+
response: {
28+
body: "로그인에 성공하였습니다.",
29+
},
30+
};
31+
32+
printLoginState(successState);
33+
34+
// Fail 타입의 값 전달 예시
35+
const failState: Fail = {
36+
result: "fail",
37+
msg: "로그인에 실패하였습니다.",
38+
};
39+
printLoginState(failState);
40+
}
Collapse file

‎docs/02. 타입.md‎

Copy file name to clipboardExpand all lines: docs/02. 타입.md
+55Lines changed: 55 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [b. 문자열 리터럴 타입으로 타입 제한](#b-문자열-리터럴-타입으로-타입-제한)
1919
- [c. 유니온 타입과 switch문으로 조건부 로직 처리](#c-유니온-타입과-switch문으로-조건부-로직-처리)
2020
- [d. 유니온 타입과 if문으로 조건부 로직 처리](#d-유니온-타입과-if문으로-조건부-로직-처리)
21+
- [9. 차별화 된 유니온 타입(Discriminated union)](#9-차별화-된-유니온-타입discriminated-union)
2122

2223
## 1. 타입의 원칙
2324

@@ -542,3 +543,57 @@ printLoginState(failState);
542543
```
543544
544545
또한, `Success` 타입과 `Fail` 타입의 값을 각각 `successState`와 `failState` 변수에 할당하고, 이 변수를 `printLoginState` 함수에 전달한다. 함수가 호출되면 `state` 매개변수에 해당 변수의 값이 전달되며, 함수 내부에서 해당 값의 타입에 따라 `'response' in state` 조건문이 참 또는 거짓이 되어, 각각에 해당하는 출력문이실행하게 된다.
546+
547+
## 9. 차별화 된 유니온 타입(Discriminated union)
548+
549+
타입스크립트에서 `discriminated union`은 여러 타입을 하나로 묶어주는 방법 중 하나이다. 이를 통해, 다양한 조건에 따라 동작이 달라지는 함수나 모듈 등을 만들 수 있다.
550+
551+
`discriminated union`의 핵심 개념은, 공통 프로퍼티를 사용하여 타입을 구분하는 것이다. 이를 통해, 코드를 더욱 명확하게 작성할 수 있고, 코드의 가독성과 유지 보수성을 향상시킬 수 있다.
552+
553+
```typescript
554+
// Success 타입과 Fail 타입을 선언합니다.
555+
type Success = {
556+
result: "success";
557+
response: {
558+
body: string;
559+
};
560+
};
561+
562+
type Fail = {
563+
result: "fail";
564+
msg: string;
565+
};
566+
567+
// Success 타입과 Fail 타입을 묶은 LoginState 타입을 선언합니다.
568+
type LoginState = Success | Fail;
569+
570+
// LoginState 타입을 처리하는 함수를 정의합니다.
571+
function printLoginState(state: LoginState) {
572+
// state.result의 값에 따라 처리 방식이 달라집니다. (원래는 'response' in state 로 작성했음)
573+
if (state.result === "success") {
574+
console.log(`🎉 ${state.response.body}`);
575+
} else {
576+
console.log(`😢 ${state.msg}`);
577+
}
578+
}
579+
580+
// Success 타입의 값 전달 예시
581+
const successState: Success = {
582+
result: "success",
583+
response: {
584+
body: "로그인에 성공하였습니다.",
585+
},
586+
};
587+
588+
printLoginState(successState);
589+
590+
// Fail 타입의 값 전달 예시
591+
const failState: Fail = {
592+
result: "fail",
593+
msg: "로그인에 실패하였습니다.",
594+
};
595+
596+
printLoginState(failState);
597+
```
598+
599+
위 코드에서는 `result`라는 공통 프로퍼티를 사용하여 `Success` 타입과 `Fail` 타입을 구분하고, 이를 `LoginState` 타입으로 묶어주었다. `printLoginState` 함수에서는 `state.result`의 값에 따라 처리 방식이 달라지도록 작성되어 있다. 이러한 코드 작성 방식은 가독성과 유지 보수성을 높이는 효과가 있다.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.