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

重写的静态函数扩展类型参数的流类型检查

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

    代码可以找到,但我不知道如何让flow在这里快乐。

    这是一个小盒子 example :

    // @flow
    
    type Data = {
      id: number 
    }
    
    type ExtendedData = Data & {
      name: string 
    }
    
    class SomeClass {
        static something(some: Data): this {
            return new this(); // returns SomeClass
        }
    }
    
    class SomeOtherClass extends SomeClass {
        // flow: Cannot extend `SomeClass` [1] with `SomeOtherClass` 
        // because property `name` is missing in `Data` [2] but exists in 
        // object type [3] in the first argument of property `something`.
        static something(some: ExtendedData): this {
            return /* returns SomeOtherClass here */ super.something(some);
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Alejandro    7 年前

    点流抱怨 is similar to this one ,您的实现中断 L 在里面 SOLID

    const f = (S: Class<SomeClass>) => {
       S.something({id: 1}) 
    }
    f(SomeClass); // ok
    f(SomeOtherClass); // fail, no name property
    

    你只能这样说他 Data 对我来说也可以 SomeOtherClass he'll be happy :

    static something(some: Data | ExtendedData): this {...}