代码之家  ›  专栏  ›  技术社区  ›  ThomasReggi

动态创建多种类型

  •  1
  • ThomasReggi  · 技术社区  · 7 年前

    我目前在许多文件中都有这样的关系,我正在寻找一种方法来创建一个组合函数,使它们之间的关系不容易出现人为错误。

    type XRaw = {};
    type X = Merge<XRaw, {}>;
    type XProto = Merge<X, {}>;
    type XProtoPrepped = Merge<XProto, {}>;
    

    像这样:

    type [XRaw, X, XProto, XProtoPrepped] = Build<{}, {}, {}, {}>
    

    我还想限制 {} 只有原始属性 XRaw ,并且不允许输入无法识别的属性。

    这有可能吗?

    2 回复  |  直到 7 年前
        1
  •  0
  •   basarat    7 年前

    我还希望限制仅在原始xraw中具有属性,并且不允许输入未识别的属性。

    听起来像你想要的 exact 类型。

    遗憾的是,typescript还不支持确切的类型。以下是排版团队提出的相关问题: https://github.com/Microsoft/TypeScript/issues/12936 γ射线

        2
  •  0
  •   ThomasReggi    7 年前
    interface Discover <Raw, RawOverrides, Proto, ProtoOverrides> {
      Raw: Raw;
      RawOverrides: Merge<Raw, RawOverrides>;
      Proto: Merge<Proto, Merge<Raw, RawOverrides>>;
      ProtoOverrides: Merge<ProtoOverrides, Merge<Proto, Merge<Raw, RawOverrides>>>;
    }
    
    type CatDiscover = Discover<{
      id: string;
    }, {}, {}, {}>
    
    type CatRaw = CatDiscover['Raw'];
    type Cat = CatDiscover['RawOverrides'];
    type CatProto = CatDiscover['Proto'];
    type CatProtoPrepped = CatDiscover['ProtoOverrides'];
    
    推荐文章