- Generics
- KeyOf Type Operator
- TypeOf Type Operator
- Indexed Access Types
- Conditional Types
- Mapped Types
Creating Types from Types 基于类型创建类型
TypeScript’s type system is very powerful because it allows expressing types in terms of(依据;在…方面;从…角度看;根据…来说) other types.
TypeScript的类型系统非常强大,因为它允许用其他类型表示类型。
The simplest form of this idea is generics, we actually have a wide variety of type operators available to use. It’s also possible to express types in terms of values that we already have.
这种思想最简单的形式是泛型,实际上我们有各种类型的运算符可供使用。也可以根据我们已有的值来表示类型。
By combining(组合) various type operators, we can express complex operations and values in a succinct(简洁的), maintainable(可维护的) way. In this section we’ll cover ways to express a new type in terms of an existing type or value.
通过组合各种类型的运算符,我们可以以简洁、可维护的方式表达复杂的操作和值。在本节中,我们将介绍用现有类型或值表示新类型的方法。
- Generics - Types which take parameters 采用参数的类型
- Keyof Type Operator - Using the
keyofoperator to create new types - Typeof Type Operator - Using the
typeofoperator to create new types - Indexed Access Types - Using
Type['a']syntax to access a subset of a type 访问类型的子集 - Conditional Types - Types which act like if statements in the type system 行为像if语句
- Mapped Types - Creating types by mapping each property in an existing type
- Template Literal Types - Mapped types which change properties via template literal strings 模板字面量字符串