Skip to content

Translate 1 file to ko, playground - Variadic Tuples #92

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/playground/ko/4-0/New TS Features/Variadic Tuples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// { "compiler": { "ts": "4.0.2" } }
// 가변 튜플은 제네릭이 동작하는 방식처럼 타입을 타입 검사기에 전달하기 위해
// 나머지 연산자(...)를 처리하는 기능을 튜플에게 제공합니다.

// 꽤 고급 주제라서 이해하기 어렵더라도 너무 걱정하지 마세요.
// example:generic-functions 그리고 example:tuples를 기반으로 합니다.

// 먼저 튜플 앞에 항상 숫자를 붙이는
// 가변 튜플이 있습니다.

type AddMax<T extends unknown[]> = [max: number, ...rest: T];
// ^ T를 제한하기 위해 사용한 제네릭
// ^ 어디에 병합하는지 나타내기 위해 사용한 ...

// 합성할 때 사용 할 수 있습니다:
type MaxMin = AddMax<[min: number]>
type MaxMinDiameter = AddMax<[min: number, diameter: number]>

// 튜플 뒤에도 동일하게 사용 할 수 있습니다:
type SuffixDIContext<T extends unknown[]> = [...first: T, context: any];
type DIContainer = SuffixDIContext<[param: string]>

// 이 메커니즘은 여러 개의 입력 매개변수와 합칠 수 있습니다.
// 예를 들어, 이 함수는 두 개의 배열을 병합하지만
// 배열이 시작, 중단하는 곳을 나타내기 위해 '\0' 기호를 사용합니다.
function joinWithNullTerminators<T extends unknown[], U extends unknown[]>(t: [...T], u: [...U]) {
return ['\0', ...t, '\0', ...u, '\0'] as const;
}

// TypeScript는 다음과 같이 함수의 반환 타입을 추론할 수 있습니다:
const result = joinWithNullTerminators(['variadic', 'types'], ["terminators", 3]);

// 이런 도구를 사용하여 함수형 프로그래밍에서 자주 사용되는 개념인
// curry같은 함수의 타입을 올바르게 지정할 수 있습니다.

function curry<T extends unknown[], U extends unknown[], R>(f: (...args: [...T, ...U]) => R, ...a: T) {
return (...b: U) => f(...a, ...b);
}

// 세 가지 제네릭 인수가 있습니다:
// - T: curry 함수에 입력 배열인 매개변수
// - U: curry 함수에 _전달되지 않고_ 반환 함수에 적용하기 위해 필요한 매개변수
// - R: 함수에 전달한 반환 타입

const sum = (left: number, right: number,) => left + right

const a = curry(sum, 1, 2)
const b = curry(sum, 1)(2)
const c = curry(sum)(1, 2)

// 여기에서 더 많은 코드 예시와 자세한 설명을 확인할 수 있습니다.
// https://github.com/microsoft/TypeScript/pull/39094