代码之家  ›  专栏  ›  技术社区  ›  Ben Aston

这在React代码库中做什么?

  •  0
  • Ben Aston  · 技术社区  · 9 年前

    我在React代码库中看到以下内容:

    export type TypeOfWork = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
    

    资料来源: https://github.com/facebook/react/blob/master/src/shared/ReactTypeOfWork.js

    1 回复  |  直到 9 年前
        1
  •  1
  •   jantimon    9 年前

    这是流或类型脚本语法。

    它定义了一个自定义类型,该类型只能是0到10之间的数字。

    export type TypeOfWork = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
    let x: TypeOfWork = 0;
    
    x = 12; // <-- compiler would throw an error here
    

    https://www.typescriptlang.org/docs/handbook/enums.html

    export const enum TypeOfWork {
      IndeterminateComponent = 0, // Before we know whether it is functional or class
      FunctionalComponent = 1,
      ClassComponent = 2,
      HostRoot = 3, // Root of a host tree. Could be nested inside another node.
      HostPortal = 4, // A subtree. Could be an entry point to a different renderer.
      HostComponent = 5,
      HostText = 6,
      CoroutineComponent = 7,
      CoroutineHandlerPhase = 8,
      YieldComponent = 9,
      Fragment = 10
    };
    

    正如您在 compiled view