Skip to content

Commit c4ded0a

Browse files
author
Orta Therox
authored
Merge pull request #86 from yahma25/Translation-to-ko-playground-Nominal-Typing
Translate 1 file to ko, playground - Nominal Typing
2 parents 0a63617 + e7747ba commit c4ded0a

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// 명목적 타입 시스템은 각 타입이 고유하고,
2+
// 타입이 같은 데이터를 가지고 있어도
3+
// 타입 간에 할당할 수 없는 것을 의미합니다.
4+
5+
// TypeScript의 타입 시스템은 구조적이어서,
6+
// 타입이 오리 같은 형태를 갖췄다면 오리라고 할 수 있습니다.
7+
// 거위가 오리처럼 같은 속성을 모두 가졌다면 이 역시 오리라고 할 수 있습니다.
8+
// 여기서 더 자세히 배우실 수 있습니다: example:structural-typing
9+
10+
// 문제점이 있을 수 있는데,
11+
// 예를 들어 문자열 또는 숫자는 특별한 컨텍스트를 갖고 있고
12+
// 값을 넘겨줄 수 있게 하고 싶지 않은 경우가 있습니다.
13+
// 예시:
14+
//
15+
// - 사용자 입력 문자열 (유효하지 않은)
16+
// - 변환 문자열
17+
// - 사용자 식별 숫자
18+
// - 액세스 토큰
19+
20+
// 코드를 조금 추가하여,
21+
// 명목적 타입 시스템에서 대부분의 값을 가져올 수 있습니다.
22+
23+
// 일반 문자열을 ValidatedInputString에
24+
// 할당할 수 없게 하는 __brand(관습입니다)라는
25+
// 프로퍼티 형식의 고유 제약으로
26+
// 교차하는 타입을 사용할 것입니다.
27+
28+
type ValidatedInputString = string & { __brand: "User Input Post Validation" };
29+
30+
// 문자열을 ValidatedInputString로 변환하기 위해
31+
// 함수를 사용하지만,
32+
// 주목할 점은 TypeScript에게 참이라고 _알려주는 것_ 입니다.
33+
34+
const validateUserInput = (input: string) => {
35+
const simpleValidatedInput = input.replace(/\</g, "≤");
36+
return simpleValidatedInput as ValidatedInputString;
37+
};
38+
39+
// 이제 일반 문자열 타입이 아닌 새로운 명목적 타입만
40+
// 허용하는 함수를 만들 수 있습니다.
41+
42+
const printName = (name: ValidatedInputString) => {
43+
console.log(name);
44+
};
45+
46+
// 예를 들어, 유효하지 않은 사용자의 입력값이 있으며
47+
// 검사기를 거치고 나면 출력해줍니다:
48+
49+
const input = "\n<script>alert('bobby tables')</script>";
50+
const validatedInput = validateUserInput(input);
51+
printName(validatedInput);
52+
53+
// 반면에, 유효하지 않은 문자열을
54+
// printName에 전달하면 컴파일러 오류가 발생합니다:
55+
56+
printName(input);
57+
58+
// 400개의 댓글이 있는 Github 이슈에서
59+
// 명목적 타입을 만드는 다른 방법과 장단점에 대한
60+
// 종합적인 개요를 읽어볼 수 있습니다:
61+
//
62+
// https://github.com/Microsoft/TypeScript/issues/202
63+
//
64+
// 그리고 훌륭한 요약 글이 있습니다:
65+
//
66+
// https://michalzalecki.com/nominal-typing-in-typescript/

0 commit comments

Comments
 (0)