Flowtype:基于类型提示类的HOC组件

时间:2018-02-27 11:40:50

标签: javascript reactjs flowtype

我正在尝试输入一个HOC,它为传递的Component添加一个prop,如下所示:

// @flow
import React, { Component } from 'react';
import type { ComponentType } from 'react';

type Props = {
  x: string,
}

const withReferral = <PassedProps: {}>(
    WrappedComponent: ComponentType<PassedProps>
): ComponentType<$Diff<PassedProps, Props>> => {
  class withReferral extends Component<PassedProps> {
    render() {
      return (<WrappedComponent {...this.props} x={'test'} />);
    }
  }

  return withReferral;
};

export default withReferral;

我得到的错误是这样的: “无法返回withReferral,因为在withReferral [1]的静态中缺少可调用签名但存在于 React.StatelessFunctionalComponent [2]。“

引用[{1]引用{2}和[2]引用React定义:return withReferral

任何有帮助的人?

1 个答案:

答案 0 :(得分:5)

您错过了WrappedComponents道具中可能缺少x的类型。如Flow HOC docs中所述,注入属性的类型需要是void的联合,例如x: string | void// @flow import React, { Component } from 'react'; import type { ComponentType } from 'react'; type Props = { x: string | void, // <<== HERE } const withReferral = <PassedProps: {}>( WrappedComponent: ComponentType<PassedProps> ): ComponentType<$Diff<PassedProps, Props>> => { class withReferral extends Component<PassedProps> { render() { return (<WrappedComponent {...this.props} x={'test'} />); } } return withReferral; }; export default withReferral;

var ErrNotExists = errors.New("value doesn't exist")
var ErrWrongType = errors.New("variable is wrong type")

type MyMap map[string]interface{}

func(m MyMap) IsArray(key string) bool {
    val, ok := m[key]
    if !ok {
        return false
    }
    _, ok = val.([]string)
    return ok
}

func(m MyMap) Exists(key string) bool {
    _, ok := m[key]
    return ok
}

func(m MyMap) SetString(key, value string) {
    m[key] = value
}

func(m MyMap) SetArray(key string, value []string) {
    m[key] = value
}

func(m MyMap) GetString(key string) (string, error) {
    v, ok := m[key]
    if !ok {
        return "", ErrNotExists
    }
    realVal, ok := v.(string)
    if !ok {
        return "", ErrWrongType
    }
    return realVal, nil
}

func(m MyMap) GetArray(key string) ([]string, error) {
    v, ok := m[key]
    if !ok {
        return nil, ErrNotExists
    }
    realVal, ok := v.([]string)
    if !ok {
        return nil, ErrWrongType
    }
    return realVal, nil
}

func(m MyMap) AppendArray(key string, toAppend []string) error {
    v, ok := m[key]
    if !ok {
        return ErrNotExists
    }
    realVal, ok := v.([]string)
    if !ok {
        return ErrWrongType
    }
    m[key] = append(realVal, toAppend...)
    return nil
}

func main() {
    m := make(MyMap)
    m.SetString("string","hello")
    m.SetArray("array",[]string{"hi","there"})
    fmt.Println(m)
    fmt.Println(m.IsArray("string"), m.IsArray("array"))
    s, err := m.GetString("string")
    if err != nil { // handle
        fmt.Println(err)
    }
    a, err := m.GetArray("array")
    if err != nil { // handle
        fmt.Println(err)
    }
    fmt.Println(s,a)
    m.AppendArray("array", []string{"all","you","people"})
    a, err = m.GetArray("array")
    if err != nil { // handle
        fmt.Println(err)
    }
    fmt.Println(a)
}

Try