Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow: Missing type annotation for T

We have a pure component, and we are trying to convert this to the Flow type safety. The application is using this component as a HOC ( High-Order Component). It is generating a context and injecting to the dispatched components.

Therefore, one method of the HOC is returning an object literal which is involving many binding operations. In this key-value pairs, there is one expression we didn't handle.

We are getting an error about missing notation:

Missing type annotation for T. T is a type parameter declared in array type [1] and was implicitly instantiated at call of method filter [2].

export type PropsType = {
  reviewConf ? : Object,
 ...
}

export type ContextType = {
  registerComponent: () => void,
  errors ? : Array < any >
  ...
}

export type StateType = {
  meta: Object
}

class AbstractPureFormComponent extends React.PureComponent < PropsType, StateType > {
    constructor(props: PropsType, context: ContextType) {
      super(props, context)

      this.state = {
        meta: {}
      }

    }
    getChildContext() {
        return {
          registerComponent: this.registerComponent.bind(this),
          ...



          errors: Object.keys(this.state.meta).filter(
                  name => this.state && this.state.meta[name].error
          )
       }
     }
   }
}

So what is the best practice for typing this error: key? Should it be an interface or type or something else...

like image 332
oyilmaztekin Avatar asked Nov 15 '18 09:11

oyilmaztekin


1 Answers

I just encountered this issue with a function that simply clones and sorts an array, e.g.

const sortArray = (myArr: MyArray[]) => [...myArr].sort((a, b) => a > b ? -1 : 1)

What solved it for me was simply typing the return value of sortArray:

const sortArray = (myArr: MyArray[]): MyArray[] => [...myArr].sort((a, b) => a > b ? -1 : 1)
like image 100
maxedison Avatar answered Oct 18 '22 04:10

maxedison