【问题标题】:how to extend object types for flow如何为流扩展对象类型
【发布时间】:2017-11-11 22:54:57
【问题描述】:

我希望能够通过添加更多字段(props C)来扩展包装组件的组件的 props A。当我使用扩展运算符时,流程会出错。

type A = {a: string}
type C = {b: number} & A //{b: number, a:string} 
const c : C = {a: 'a', b: 1}
const {b, ...a} = c;
const a2 : A = a;

这会导致流程错误 6: 常量 a2 : A = a; ^ 对象模式的其余部分。

解决办法是什么?

https://flow.org/try/#0C4TwDgpgBAglC8UDeBDAXFAzsATgSwDsBzAXwChRIoBhBZAIwwIFcBbeiHEqAMligD0ApIygt2nADRR02fMW5kyAYwD2BbFGVQMtRKgwByFIemiAjOTUbgDaQDpHKbomUBuFes0oATDv6IKG5AA

【问题讨论】:

    标签: reactjs flowtype


    【解决方案1】:

    解决方法如下:

    type A = {a: string}
    type C = {b: number} & A //{b: number, a:string} 
    
    const c : C = {a: 'a', b: 1}
    const {b, a} = c;
    const a2 : A = {a};
    

    链接到:FLOW

    【讨论】:

    • 感谢您的解决方法。但是,如果 C 有 10 个字段而不是 1 个字段,则会更加冗长。
    【解决方案2】:

    flow 的展开运算符有很多问题,like you can see in the issues
    目前最好的解决方法是显式提取每个属性,就像 Vivek Doshi 所说的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多