Fancy Front End Fancy Front End
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

让有意义的事变得有意思,让有意思的事变得有意义
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • typescript-utility

    • 开始阅读
    • 基础

    • 类型工具

      • 本章概要
      • Basic Utility Types
        • 目录
        • $Keys
        • $Values
        • $ReadOnly
        • $Diff
        • $PropertyType
        • $ElementType
        • $Call
        • $Shape
        • $NonMaybeType
        • Class
      • Mapped Types
  • single-spa源码

  • qiankun源码

  • webpack

  • axios

  • solid

  • vite源码

  • jquery源码

  • snabbdom

  • am-editor

  • html2canvas

  • express

  • acorn源码

  • immutable.js源码

  • web
  • typescript-utility
  • 类型工具
jonsam
2022-04-14
目录

Basic Utility Types

# 目录

  • 目录
  • $Keys
  • $Values
  • $ReadOnly
  • $Diff
  • $PropertyType
  • $ElementType
  • $Call
  • $Shape
  • $NonMaybeType
  • Class

以下方法兼容 Flow (opens new window) 类型。

# $Keys

获取 object 的 keys。

/**
 * $Keys
 * @desc Get the union type of all the keys in an object type `T`
 * @see https://flow.org/en/docs/types/utilities/#toc-keys
 * @example
 *   type Props = { name: string; age: number; visible: boolean };
 *
 *   // Expect: "name" | "age" | "visible"
 *   type PropsKeys = $Keys<Props>;
 */
export type $Keys<T extends object> = keyof T;
1
2
3
4
5
6
7
8
9
10
11

# $Values

获取 object 的 values types。

/**
 * $Values
 * @desc Get the union type of all the values in an object type `T`
 * @see https://flow.org/en/docs/types/utilities/#toc-values
 * @example
 *   type Props = { name: string; age: number; visible: boolean };
 *
 *   // Expect: string | number | boolean
 *   type PropsValues = $Values<Props>;
 */
export type $Values<T extends object> = T[keyof T];
1
2
3
4
5
6
7
8
9
10
11

# $ReadOnly

获取 deep readonly object type。

/**
 * $ReadOnly
 * @desc Get the read-only version of a given object type `T` (it works on nested data structure)
 * @see https://flow.org/en/docs/types/utilities/#toc-readonly
 * @example
 *   type Props = { name: string; age: number; visible: boolean };
 *
 *   // Expect: Readonly<{ name: string; age: number; visible: boolean; }>
 *   type ReadOnlyProps = $ReadOnly<Props>;
 */
export type $ReadOnly<T extends object> = DeepReadonly<T>;
1
2
3
4
5
6
7
8
9
10
11
  • DeepReadonly:object deep readonly type.

# $Diff

获取两个 object 的类型差集。

/**
 * $Diff
 * @desc Get the set difference of a given object types `T` and `U` (`T \ U`)
 * @see https://flow.org/en/docs/types/utilities/#toc-diff
 * @example
 *   type Props = { name: string; age: number; visible: boolean };
 *   type DefaultProps = { age: number };
 *
 *   // Expect: { name: string; visible: boolean; }
 *   type RequiredProps = Diff<Props, DefaultProps>;
 */
export type $Diff<T extends U, U extends object> = Pick<
  T,
  SetComplement<keyof T, keyof U>
>;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • SetComplement:从集合中过滤掉另一个集成中的类型。

# $PropertyType

获取 object 中某个 key 对应的 type。

/**
 * $PropertyType
 * @desc Get the type of property of an object at a given key `K`
 * @see https://flow.org/en/docs/types/utilities/#toc-propertytype
 * @example
 *   // Expect: string;
 *   type Props = { name: string; age: number; visible: boolean };
 *   type NameType = $PropertyType<Props, 'name'>;
 *
 *   // Expect: boolean
 *   type Tuple = [boolean, number];
 *   type A = $PropertyType<Tuple, '0'>;
 *   // Expect: number
 *   type B = $PropertyType<Tuple, '1'>;
 */
export type $PropertyType<T extends object, K extends keyof T> = T[K];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# $ElementType

给定 index 的类型,获取 object、array、tuple 指定下标的类型。

/**
 * $ElementType
 * @desc Get the type of elements inside of array, tuple or object of type `T`, that matches the given index type `K`
 * @see https://flow.org/en/docs/types/utilities/#toc-elementtype
 * @example
 *   // Expect: string;
 *   type Props = { name: string; age: number; visible: boolean };
 *   type NameType = $ElementType<Props, 'name'>;
 *
 *   // Expect: boolean
 *   type Tuple = [boolean, number];
 *   type A = $ElementType<Tuple, '0'>;
 *   // Expect: number
 *   type B = $ElementType<Tuple, '1'>;
 *
 *   // Expect: boolean
 *   type Arr = boolean[];
 *   type ItemsType = $ElementType<Arr, number>;
 *
 *   // Expect: number
 *   type Obj = { [key: string]: number };
 *   type ValuesType = $ElementType<Obj, string>;
 */
export type $ElementType<
  T extends { [P in K & any]: any },
  K extends keyof T | number
> = T[K];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# $Call

获取函数的返回值类型。

/**
 * $Call
 * @desc Get the return type from a given typeof expression
 * @see https://flow.org/en/docs/types/utilities/#toc-call
 * @example
 *   // Common use-case
 *   const add = (amount: number) => ({ type: 'ADD' as 'ADD', payload: amount });
 *   type AddAction = $Call<typeof returnOfIncrement>; // { type: 'ADD'; payload: number }
 *
 *   // Examples migrated from Flow docs
 *   type ExtractPropType<T extends { prop: any }> = (arg: T) => T['prop'];
 *   type Obj = { prop: number };
 *   type PropType = $Call<ExtractPropType<Obj>>; // number
 *
 *   type ExtractReturnType<T extends () => any> = (arg: T) => ReturnType<T>;
 *   type Fn = () => number;
 *   type FnReturnType = $Call<ExtractReturnType<Fn>>; // number
 */
export type $Call<Fn extends (...args: any[]) => any> = Fn extends (
  arg: any
) => infer RT
  ? RT
  : never;

type ReturnType<T> = T extends (...args: any[]) => infer P ? P : any;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  • infer: 表示在 extends 条件语句中待推断的类型变量。

# $Shape

使 object 的所有 property 类型为可选。

/**
 * $Shape
 * @desc Copies the shape of the type supplied, but marks every field optional.
 * @see https://flow.org/en/docs/types/utilities/#toc-shape
 * @example
 *   type Props = { name: string; age: number; visible: boolean };
 *
 *   // Expect: Partial<Props>
 *   type PartialProps = $Shape<Props>;
 */
export type $Shape<T extends object> = Partial<T>;
1
2
3
4
5
6
7
8
9
10
11

# $NonMaybeType

去除 null 、undefined 类型。

/**
 * $NonMaybeType
 * @desc Excludes null and undefined from T
 * @see https://flow.org/en/docs/types/utilities/#toc-nonmaybe
 * @example
 *   type MaybeName = string | null;
 *
 *   // Expect: string
 *   type Name = $NonMaybeType<MaybeName>;
 */
export type $NonMaybeType<T> = NonNullable<T>;
1
2
3
4
5
6
7
8
9
10
11

# Class

获取 class 的类型。

/**
 * Class
 * @desc Represents constructor of type T
 * @see https://flow.org/en/docs/types/utilities/#toc-class
 * @example
 *   class Store {}
 *   function makeStore(storeClass: Class<Store>): Store {
 *     return new storeClass();
 *   }
 */
export type Class<T> = new (...args: any[]) => T;
1
2
3
4
5
6
7
8
9
10
11
编辑 (opens new window)
上次更新: 2022/09/06, 14:25:16
本章概要
Mapped Types

← 本章概要 Mapped Types→

最近更新
01
渲染原理之组件结构与 JSX 编译
09-07
02
计划跟踪
09-06
03
开始上手
09-06
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy Front End | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式