-
Notifications
You must be signed in to change notification settings - Fork 131
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
Translate 1 file to ko - TypeScript 4.1.md #94
Conversation
Translation of TypeScript 4.1.mdtitle: TypeScript 4.1 oneline: TypeScript 4.1 Release NotesTemplate Literal TypesIn TypeScript, string literal types enable you to model functions and APIs that anticipate a set of specific strings. // @errors: 2345
function setVerticalAlignment(location: "top" | "middle" | "bottom") {
// ...
}
setVerticalAlignment("middel"); This is pretty good in that the string literal type can spell check for string values by default. type Options = {
[K in "noImplicitAny" | "strictNullChecks" | "strictFunctionTypes"]?: boolean;
};
// same as
// type Options = {
// noImplicitAny?: boolean,
// strictNullChecks?: boolean,
// strictFunctionTypes?: boolean
// }; However, there are other cases where you can use those string literal types like architectural blocks: when you create other string literal types, For the above reasons, Typescript 4.1 added a template literal string type. type World = "world";
type Greeting = `hello ${World}`;
// ^? What happens if you use it instead of a union type? This generates a set of all string literals that each union members can represent. type Color = "red" | "blue";
type Quantity = "one" | "two";
type SeussFish = `${Quantity | Color} fish`;
// ^? These can be used in more places than cute examples in release notes. For example, various UI component libraries often provide a way to align vertically and horizontally, often // @errors: 2345
type VerticalAlignment = "top" | "middle" | "bottom";
type HorizontalAlignment = "left" | "center" | "right";
// Takes
// | "top-left" | "top-center" | "top-right"
// | "middle-left" | "middle-center" | "middle-right"
// | "bottom-left" | "bottom-center" | "bottom-right"
declare function setAlignment(value: `${VerticalAlignment}-${HorizontalAlignment}`): void;
setAlignment("top-left"); // works!
setAlignment("top-middel"); // error!
setAlignment("top-pot"); // error! but good doughnuts if you're ever in Seattle An example of sorting the UI API A lot Despite this, this is still only a simple example in that we write it passively. The real value of this comes from dynamically creating new string literals. let person = makeWatchedObject({
firstName: "Homer",
age: 42, // give-or-take
location: "Springfield",
});
person.on("firstNameChanged", () => {
console.log(`firstName was changed!`);
}); Where the on method is How do I type the on method? type PropEventSource<T> = {
on(eventName: `${string & keyof T}Changed`, callback: () => void): void;
};
/// 감시 대상 객체를 on 메소드와 함께 만들어서
/// 프로퍼티의 변화를 감시할 수 있게됩니다.
declare function makeWatchedObject<T>(obj: T): T & PropEventSource<T>; As such, if an incorrect value comes in, you can create an error! // @errors: 2345
type PropEventSource<T> = {
on(eventName: `${string & keyof T}Changed`, callback: () => void): void;
};
declare function makeWatchedObject<T>(obj: T): T & PropEventSource<T>;
let person = makeWatchedObject({
firstName: "Homer",
age: 42, // 대략
location: "Springfield",
});
// ---생략---
// error!
person.on("firstName", () => {});
// error!
person.on("frstNameChanged", () => {}); You can also use template literal types to do something special. : Inferred at the position of substitution (infer) In the example above, you can identify the relevant type PropEventSource<T> = {
on<K extends string & keyof T>
(eventName: `${K}Changed`, callback: (newValue: T[K]) => void ): void;
};
declare function makeWatchedObject<T>(obj: T): T & PropEventSource<T>;
let person = makeWatchedObject({
firstName: "Homer",
age: 42,
location: "Springfield",
});
// 작동합니다! 'newName'의 타입은 'string'입니다.
person.on("firstNameChanged", newName => {
// 'newName'의 타입은 'fistName'의 타입과 같습니다.
console.log(`new name is ${newName.toUpperCase()}`);
});
// 작동합니다! 'newAge'의 타입은 'number'입니다.
person.on("ageChanged", newAge => {
if (newAge < 0) {
console.log("warning! negative age");
}
}) In generic methods Inference can be combined in different ways to decompose and reconstruct strings. type EnthusiasticGreeting<T extends string> = `${Uppercase<T>}`
type HELLO = EnthusiasticGreeting<"hello">;
// ^? New type aliases The front two replace all characters, and the back two replace only the first character. For more information, see Check the original pull request. and Ongoing pull request to replace with type alias helperCheck it out too. Key Remapping in Mapped TypesFirst of all, the mapping type is used to create new objects based on arbitrary keys, type Options = {
[K in "noImplicitAny" | "strictNullChecks" | "strictFunctionTypes"]?: boolean;
};
// same as
// type Options = {
// noImplicitAny?: boolean,
// strictNullChecks?: boolean,
// strictFunctionTypes?: boolean
// }; Alternatively, you can create new object types based on different object types. /// 'Partial<T>'가 'T'와 같지만, 각 프로퍼티는 선택 사항으로 표시되어 있음.
type Partial<T> = {
[K in keyof T]?: T[K];
}; Until now, mapped types had to have keys provided to create new object types. However, in most cases, you can create a new key based on input, or filter the key. This is why Typescript 4.1 type MappedTypeWithNewKeys<T> = {
[K in keyof T as NewKeyType]: T[K]
// ^^^^^^^^^^^^^
// 이것이 새 문법입니다!
} This new type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K]
};
interface Person {
name: string;
age: number;
location: string;
}
type LazyPerson = Getters<Person>;
// ^? and // 'kind' 프로퍼티를 삭제하기
type RemoveKindField<T> = {
[K in keyof T as Exclude<K, "kind">]: T[K]
};
interface Circle {
kind: "circle";
radius: number;
}
type KindlessCircle = RemoveKindField<Circle>;
// ^? For more information, see: Original pull request on GithubCheck it out. Recursive Condition TypesIn JavaScript, functions that can be created and leveled container types at any level are common. For all practical intents and purposes, it is impossible to describe this in type systems in TypeScript. type ElementType<T> = T extends ReadonlyArray<infer U> ? ElementType<U> : T;
function deepFlatten<T extends readonly unknown[]>(x: T): ElementType<T>[] {
throw "not implemented";
}
// 모두 'number[]' 타입을 반환합니다.
deepFlatten([1, 2, 3]);
deepFlatten([[1], [2, 3]]);
deepFlatten([[1], [[2]], [[[3]]]]); Similarly, in TypeScript 4.1, type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T;
/// `promise.then(...)`과 비슷하지만, 타입에 대해서는 더 정확함.
declare function customThen<T, U>(
p: Promise<T>,
onFulfilled: (value: Awaited<T>) => U
): Promise<Awaited<U>>; These recursive types are powerful but responsible and should be used with less. First, these recursive types do a lot of work, thus increasing the type verification time. In addition, leaving compute-intensive, these types can reach the internal recursive depth limit for sufficiently complex inputs. SupplementsSee . Checked Indexed Accesses (
|
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.
@minsoo0715 번역 감사합니다. :) 전반적으로 문단 구분을 원본과 맞춰야 할 것 같습니다.
@minsoo0715 고생하셨습니다 :) 아직 전부다 자세히 읽어보진 않았습니다. 눈에 띄는 부분 코멘트 남겼습니다 👍 |
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
꼼꼼한 리뷰 감사드립니다. 🙏 사소한 결점들이 너무 많네요. |
en
TypeScript 4.1.md 번역완료했습니다.
어색한 부분이 있거나 틀린 부분있으면 코멘트 부탁드립니다🙏
ref #6