Skip to content

Feat translate zh #161

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 2 commits into
base: main
Choose a base branch
from
Open

Conversation

17biubiu
Copy link

translate Utility Types to chinese

@ghost
Copy link

ghost commented Jul 29, 2022

CLA assistant check
All CLA requirements met.

@github-actions
Copy link
Contributor

Thanks for the PR!

This section of the codebase is owned by @Kingwl - if they write a comment saying "LGTM" then it will be merged.

@github-actions
Copy link
Contributor

Translation of Utility Types.md

title: Tool type
layout: docs
permalink: /zh/docs/handbook/utility-types.html
oneline: A type that is globally included in TypeScript

translatable: true

TypeScript provides several common types of tools that facilitate type conversion. These tool types are available globally.

Awaited<Type>

Release Version:
4.5

This type is intended to be simulated async function await or something like thatPromise on.then() Methods - Specifically, they are recursive
unfoldPromise

Example
type A = Awaited<Promise<string>>;
//   ^?

type B = Awaited<Promise<Promise<number>>>;
//   ^?

type C = Awaited<boolean | Promise<number>>;
//   ^?

Partial<Type>

Release Version:
2.1

Construct a will Type All properties are set to the type of option. This tool type returns a type that represents all subsets of a given type.

Example
interface Todo {
  title: string;
  description: string;
}

function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
  return { ...todo, ...fieldsToUpdate };
}

const todo1 = {
  title: "organize desk",
  description: "clear clutter",
};

const todo2 = updateTodo(todo1, {
  description: "throw out trash",
});

Required<Type>

Release Version:
2.8

Construct a will Type All properties are set to the type of rare. It is[Partial]The reverse operation of (#partialtype).

Example
// @errors: 2741
interface Props {
  a?: number;
  b?: string;
}

const obj: Props = { a: 5 };

const obj2: Required<Props> = { a: 5 };

Readonly<Type>

Release Version:
2.1

Construct a will Type All properties are set to readonlyof the constructed type, which means that these properties cannot be reassigned to the constructed type.

Example
// @errors: 2540
interface Todo {
  title: string;
}

const todo: Readonly<Todo> = {
  title: "Delete inactive users",
};

todo.title = "Hello";

This utility is useful for representing assignment expressions that will fail at runtime (i.e. when attempting to reassign properties of a frozen object).

This tool is useful for expressions that indicate assignment expressions that will fail at run time (that is, when an attempt is made to reassign [frozen object] When the property of the (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze))

Object.freeze
function freeze<Type>(obj: Type): Readonly<Type>;

Record<Keys, Type>

Release Version:
2.1

Constructs an object type with a property key of Keys and a property value of Type. This tool can be used to map attributes of one type to another.

Example
interface CatInfo {
  age: number;
  breed: string;
}

type CatName = "miffy" | "boris" | "mordred";

const cats: Record<CatName, CatInfo> = {
  miffy: { age: 10, breed: "Persian" },
  boris: { age: 5, breed: "Maine Coon" },
  mordred: { age: 16, breed: "British Shorthair" },
};

cats.boris;
// ^?

Pick<Type, Keys>

Release Version:
2.1

By from Type Select a set of properties Keys(string literal or a union of string literals) to construct a type.

Example
interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

type TodoPreview = Pick<Todo, "title" | "completed">;

const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
};

todo;
// ^?

Omit<Type, Keys>

Release Version:
3.5

By from Type Select all properties and delete them Keys(string literals or a union of string literals) to construct the type.

Example
interface Todo {
  title: string;
  description: string;
  completed: boolean;
  createdAt: number;
}

type TodoPreview = Omit<Todo, "description">;

const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
  createdAt: 1615544252770,
};

todo;
// ^?

type TodoInfo = Omit<Todo, "completed" | "createdAt">;

const todoInfo: TodoInfo = {
  title: "Pick up kids",
  description: "Kindergarten closes at 5pm",
};

todoInfo;
// ^?

Exclude<UnionType, ExcludedMembers>

Release Version:
2.8

Construct a type by excluding from unionType all union members that can be assigned to ExcludedMembers.

Example
type T0 = Exclude<"a" | "b" | "c", "a">;
//    ^?
type T1 = Exclude<"a" | "b" | "c", "a" | "b">;
//    ^?
type T2 = Exclude<string | number | (() => void), Function>;
//    ^?

Extract<Type, Union>

Release Version:
2.8

By from Type Extract all that can be assigned to Union A union member to construct a type.

Example
type T0 = Extract<"a" | "b" | "c", "a" | "f">;
//    ^?
type T1 = Extract<string | number | (() => void), Function>;
//    ^?

NonNullable<Type>

Release Version:
2.8

By from Type Exclude null and undefined to construct a type.

Example
type T0 = NonNullable<string | number | undefined>;
//    ^?
type T1 = NonNullable<string[] | null | undefined>;
//    ^?

Parameters<Type>

Release Version:
3.1

From the function type Type The tuple type is constructed using the type.

Example
// @errors: 2344
declare function f1(arg: { a: number; b: string }): void;

type T0 = Parameters<() => string>;
//    ^?
type T1 = Parameters<(s: string) => void>;
//    ^?
type T2 = Parameters<<T>(arg: T) => T>;
//    ^?
type T3 = Parameters<typeof f1>;
//    ^?
type T4 = Parameters<any>;
//    ^?
type T5 = Parameters<never>;
//    ^?
type T6 = Parameters<string>;
//    ^?
type T7 = Parameters<Function>;
//    ^?

ConstructorParameters<Type>

Release Version:
3.1

Constructs a tuple or array type from the type of the constructor type. It generates a tuple type that contains all parameter types (if so Type If it is not a function, the type is generated never)。

Example
// @errors: 2344
// @strict: false
type T0 = ConstructorParameters<ErrorConstructor>;
//    ^?
type T1 = ConstructorParameters<FunctionConstructor>;
//    ^?
type T2 = ConstructorParameters<RegExpConstructor>;
//    ^?
type T3 = ConstructorParameters<any>;
//    ^?

type T4 = ConstructorParameters<Function>;
//    ^?

ReturnType<Type>

Release Version:
2.8

Construct a function by Type The type of type that is returned consists of.

Example
// @errors: 2344 2344
declare function f1(): { a: number; b: string };

type T0 = ReturnType<() => string>;
//    ^?
type T1 = ReturnType<(s: string) => void>;
//    ^?
type T2 = ReturnType<<T>() => T>;
//    ^?
type T3 = ReturnType<<T extends U, U extends number[]>() => T>;
//    ^?
type T4 = ReturnType<typeof f1>;
//    ^?
type T5 = ReturnType<any>;
//    ^?
type T6 = ReturnType<never>;
//    ^?
type T7 = ReturnType<string>;
//    ^?
type T8 = ReturnType<Function>;
//    ^?

InstanceType<Type>

Release Version:
2.8

Constructs a type that is used by Type The constructor consists of instance types in the constructor.

Example
// @errors: 2344 2344
// @strict: false
class C {
  x = 0;
  y = 0;
}

type T0 = InstanceType<typeof C>;
//    ^?
type T1 = InstanceType<any>;
//    ^?
type T2 = InstanceType<never>;
//    ^?
type T3 = InstanceType<string>;
//    ^?
type T4 = InstanceType<Function>;
//    ^?

ThisParameterType<Type>

Release Version:
3.3

If the function type is not this parameters, then extract the function type this Parameter or [unknown](/docs/handbook/release-notes/typescript-3-0.html# new-unknown-top-type) type.

Example
function toHex(this: Number) {
  return this.toString(16);
}

function numberToString(n: ThisParameterType<typeof toHex>) {
  return toHex.apply(n);
}

OmitThisParameter<Type>

Release Version:
3.3

from Type delete this Parameter. If Type There is no explicit statement this parameters, then the result is only Type。 Otherwise, from Type Create one that does not this The new function type of the parameter. After the generic is removed, only the last overloaded signature is passed to the new function type.

Example
function toHex(this: Number) {
  return this.toString(16);
}

const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind(5);

console.log(fiveToHex());

ThisType<Type>

Release Version:
2.3

This tool does not return the converted type. Instead, it is used as a context this The type of tag. Note that it must be enabled noImplicitThis flags are required to use this tool.

Example
// @noImplicitThis: false
type ObjectDescriptor<D, M> = {
  data?: D;
  methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M
};

function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M {
  let data: object = desc.data || {};
  let methods: object = desc.methods || {};
  return { ...data, ...methods } as D & M;
}

let obj = makeObject({
  data: { x: 0, y: 0 },
  methods: {
    moveBy(dx: number, dy: number) {
      this.x += dx; // Strongly typed this
      this.y += dy; // Strongly typed this
    },
  },
});

obj.x = 10;
obj.y = 20;
obj.moveBy(5, 5);

In the example above,makeObject parameter methods The object has a containment ThisType<D & M> The context type, therefore this at methods Method is { x: number, y: number } & { moveBy(dx: number, dy: number): number }consortium. note methods How the type of the property becomes in a method this The type of inference target and source.

ThisType<T> Tag the interface just inlib.d.ts An empty interface declared in . Except for being recognized in the context type of the object literal, the interface behaves like any empty interface.

Intrinsic String Manipulation Types

Uppercase<StringType>

Lowercase<StringType>

Capitalize<StringType>

Uncapitalize<StringType>

TypeScript contains a set of types that can be used for string-type operations within the system to facilitate operations using template strings. You can Template Literal Types Find these in the documentation.

Generated by 🚫 dangerJS against 5d19278

@awxiaoxian2020
Copy link
Contributor

squash two commits seems good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants